Test that array with both selection sort and bubble sort.Compute the time taken for each algorithm to complete.Repeat step two and three for 1000, 10000, 100000, and 1000000 random numbers.Plot the results in a graph
#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void selectionSort(int array1[], int n)
{
int min;
for (int i = 0; i < n-1; i++)
{
min = i;
for (int j = i+1; j < n; j++)
if (array1[j] < array1[min])
min = j;
swap(&array1[min], &array1[i]);
}
}
void display(int array1[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", array1[i]);
printf("\n");
}
int main()
{
int array1[] = {9,4,2,6,5,7,1,8,3};
int size = sizeof(array1)/sizeof(array1[0]);
selectionSort(array1, size);
printf("Sorted array: \n");
display(array1, size);
return 0;
}
Comments
Leave a comment