Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- int swap;
- void bubble_sort(int a[], int n)
- {
- int i,j,temp;
- swap=0;
- for(i=0; i<n; i++)
- {
- for(j=0; j<n-i-1; j++)
- {
- if(a[j]>a[j+1])
- {
- temp = a[j];
- a[j] = a[j+1];
- a[j+1] = temp;
- swap++;
- }
- }
- }
- }
- void insertion_sort(int a[],int n)
- {
- int i,j,temp;
- swap=0;
- for(i=0; i<n; i++)
- {
- j=i;
- while(j>=0&&a[j]>a[j+1])
- {
- temp = a[j];
- a[j] = a[j+1];
- a[j+1] = temp;
- j--;
- swap++;
- }
- }
- }
- void selection_sort(int a[],int n)
- {
- int i,j,temp,mini;
- swap=0;
- for(i=0; i<n-1; i++)
- {
- mini=i;
- for(j=i+1; j<n; j++)
- {
- if(a[mini]>a[j])
- {mini=j;
- }
- if(mini>i)
- {
- temp=a[mini];
- a[mini]=a[i];
- a[i]=temp;
- swap++;
- }
- }
- }
- }
- int main()
- {
- int arr[500005];
- int len, i;
- clock_t start_time, end_time;
- double bubble_time;
- double selection_time;
- double insertion_time;
- printf("Enter Array Length: ");
- scanf("%d", &len);
- srand(time(0));
- for(i=0; i<len; i++)
- {
- arr[i]=rand()%100+1;
- }
- start_time = clock();
- selection_sort(arr,len);
- end_time = clock();
- selection_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
- printf("\nTime for selection sort 1000: %lf\n",selection_time);
- printf("swap = %d\n",swap);
- for(i=0; i<len; i++)
- {
- arr[i]=rand()%100+1;
- }
- start_time = clock();
- bubble_sort(arr,len);
- end_time = clock();
- bubble_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
- printf("Time for bubble sort: %lf\n",bubble_time);
- printf("swap = %d\n",swap);
- ///////////
- for(i=0; i<len; i++)
- {
- arr[i]=rand()%100+1;
- }
- start_time = clock();
- insertion_sort(arr,len);
- end_time = clock();
- insertion_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
- printf("\nTime for insertion sort 1000: %lf\n",insertion_time);
- printf("swap = %d\n",swap);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement