Answer to Question #273271 in C for Amit

Question #273271

Implement insertion sort.

1
Expert's answer
2021-12-01T12:12:38-0500
#include <stdio.h>
int main()
{
	int n, i, j, temp;
	int numbers[64];


	printf("Enter number of numbers\n");
	scanf("%d", &n);


	for (i = 0; i < n; i++)
	{
		printf("Enter number %d: ", (i+1));
		scanf("%d", &numbers[i]);
	}
	for (i = 1 ; i <= n - 1; i++)
	{
		j = i;
		while ( j > 0 && numbers[j-1] > numbers[j])
		{	        
			temp     = numbers[j];
			numbers[j]   = numbers[j-1];
			numbers[j-1] = temp;
			j--;
		}
	}
	printf("Sorted list in ascending order:\n");
	for (i = 0; i <= n - 1; i++)
	{
		printf("%d\n", numbers[i]);
	}


	getchar();
	getchar();
	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