197,552 views
10 votes
10 votes
in the c/c program write a separate function for bubble sort. set up a timer using the time() function and record the time difference for the assembly and for the c/c function for sorting. to get meaningful reading, the array should have 10,000 elements or more. the two functions should be given the same, unsorted array to work with. the recorded time differences should be displayed on the console.

User Bartek Chlebek
by
2.9k points

1 Answer

4 votes
4 votes

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

//Define the size

#define SIZE 12000

//Time

time_t start, stop;

//Array

int arr[SIZE];

void bubble_sort(int a[]) {

for(int i=0;i<SIZE;i++) {

for(int j=0;j<SIZE-i;j++) {

if(a[j]>a[j+1]) {

int tmp = a[j];

arr[j] = arr[j+1];

arr[j+1] = tmp;

}

}

}

}

int main(int argc, char* argv[]) {

//Random filling

srand(time(NULL));

//Array initialization

for(int i=0;i<SIZE;i++) {

arr[i] = rand() % 100;

}

//Bubble sort

start = time(NULL);

bubble_sort(arr);

stop = time(NULL);

//Print the time difference between stop and start.

printf("Time difference is: %.2f s.\\",difftime(stop,start));

return 0;

}

in the c/c program write a separate function for bubble sort. set up a timer using-example-1
User Khawer Khaliq
by
2.4k points