C programming experiment
Its been awhile since i have used C ( 10 years or more) but I decided to write a little array program here is a description.
Im tring an experiment that will generate 7000 integer random numbers and save them in an array. Then I need to copy these 7000 values into a second array, so that I have two identical integer arrays.
In a function, I want to sort the first array with an un-optimized bubble sort.
In a second function, I want to sort the second array with an optimized bubble sort.
So in the main, I would like to print out the time each sort routine took to execute and print out a message determining which sort routine was faster.
Any Ideas, I made this one up for fun to also learn how to do arrays and counts.
Any help would be appreciated....
I can add some of what I have done if necessary. Thanks to those who help.
# 1 Re: C programming experiment
This should work for the sort part:
#include <conio.h>
#include<iostream>
#define RMAX 7000
void psort(int*,int,int);
int partition(int*,int,int);
int main(){
int randnum1[RMAX],randnum2[RMAX];
int j;
srand(time(NULL));
for(j=0;j<RMAX;j++){
randnum1[j]=rand()%RMAX+1;
randnum2[j]=randnum1[j];
printf( "Array Value %d is %d \n", j, randnum1[j]);
}
psort(randnum1,0,RMAX-1);
for(j=0;j<RMAX;j++){
printf( "Array Value %d is %d \n", j, randnum1[j]);
}
getch();
}
void psort(int arr[],int lower,int upper){
int pivot=partition(arr,lower,upper);
if(lower<pivot)psort(arr,lower,pivot-1);
if(upper>pivot)psort(arr,pivot+1,upper);
}
int partition(int arr[],int left,int right){
int swingarmaf=arr[left];
while(left<right){
while(arr[right]>=swingarmaf&&left<right)right--;
if(right!=left){
arr[left]=arr[right];
left++;
}
while(arr[left]<=swingarmaf&&left<right)left++;
if(right!=left){
arr[right]=arr[left];
right--;
}
}
arr[left]=swingarmaf;
return left;
}