Answer to Question #261152 in C for Amanda

Question #261152

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


1
Expert's answer
2021-11-04T16:28:45-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS