Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <limits.h>
- #define MAXL 100000
- #define MAXE 1000000
- void copyArray(int[], int[], int);
- void printArray(int[], int);
- void randArray(int[], int);
- int cmp(const void *, const void *);
- void quickSort(int[], int);
- void bubbleSort(int[], int);
- void selectionSort(int[], int);
- void insertionSort(int[], int);
- void shellSort(int[], int);
- void mergeSort(int[], int);
- void test_func(void (*f)(int[], int), int[], int);
- int main()
- {
- int a[MAXL], b[MAXL], i, j;
- void (*pf[])(int[], int) = {bubbleSort, selectionSort, quickSort,
- insertionSort, shellSort, mergeSort};
- char algs[][MAXL] = {"Bubble sort:", "Selection sort:", "Quicksort:",
- "Insertion sort:", "Shell sort:", "Merge sort:"};
- for (i = 10; i <= MAXL; i *= 10)
- {
- randArray(a, i);
- printf("\ni : %d\n", i);
- for (j = 0; j < 6; j++)
- {
- puts(algs[j]);
- copyArray(a, b, i);
- test_func(pf[j], b, i);
- }
- }
- return 0;
- }
- int cmp(const void *a, const void *b)
- {
- return *(int *)a > *(int *)b;
- }
- void copyArray(int a[], int b[], int n)
- {
- int i;
- for (i = 0; i < n; i++)
- {
- b[i] = a[i];
- }
- }
- int partition(int a[], int lo, int hi)
- {
- // TODO : quicksort parition procedure
- return 0;
- swap(a[j], a[iMin])
- }
- void quicksort(int a[], int lo, int hi)
- {
- // TODO : quicksort for a[lo..hi]
- }
- void quickSort(int a[], int n)
- {
- quicksort(a, 0, n - 1);
- }
- void selectionSort(int a[], int n)
- {
- // TODO
- }
- void bubbleSort(int a[], int n)
- {
- int i,j,tmp=0;
- for(i=0;i<n-1;i++)
- {
- for(j=0;j<n-i-1;j++)
- if(a[i]<a[j])
- {
- temp=a[j];
- a[j]=a[i];
- a[i]=temp;
- }
- }
- }
- void insertionSort(int a[], int n)
- {
- // TODO
- }
- void shellSort(int a[], int n)
- {
- // TODO
- }
- void merge(int a[], int p, int q, int r)
- {
- n1=
- }
- void mergesort(int a[], int p, int r)
- {
- if(p<r)
- {
- int m = p+(l-p)/2;
- mergesort(a,p,m);
- mergesort(a,m+1,r);
- merge(a,p,m,r);
- }
- }
- void mergeSort(int a[], int n)
- {
- mergesort(a, 0, n - 1);
- }
- void randArray(int a[], int n)
- {
- int i;
- srand(time(0));
- for (i = 0; i < n; i++)
- {
- a[i] = rand() % MAXE;
- }
- }
- void printArray(int a[], int n)
- {
- int i = 0;
- for (i = 0; i < n; i++)
- {
- printf("%d ", a[i]);
- }
- puts("");
- }
- void test_func(void (*f)(int[], int), int a[], int n)
- {
- clock_t start_t, end_t;
- start_t = clock();
- f(a, n);
- end_t = clock();
- double total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
- printf("Total time: %lf\n", total_t);
- //printArray(a, n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement