#include <stdio.h> #include <stdlib.h> main () { int a[10] = {3,4,1,5,6,8,8,2,4,9}; int i, j, t; for (i = 8; i >= 0; i--) { for (j = i; j < 9; j++) { if (a[j] <= a[j+1]) break; t=a[j]; a[j] = a[j+1]; a[j+1] = t; } } for (i = 0; i < 10; i++) printf ("%d ",a[i]); printf ("\nEnd\n"); }
This is sorting by inserts in ascending order.
The idea is that in the inner loop a new element is inserted into an already ordered sequence . At the first step, its length is 1. The insertion takes place through an exchange, plunging a new element into each already sorted one at each step, until it takes its place.
The outer loop supplies new items. As a function for an array of size N, I think, issue it yourself.
Which of the algorithms is called "Sort by choice" I do not remember.
If the topic is interesting, I advise you to twist 1). Sorting inserts into the tree (the list structure is ordered at each step. It is convenient when searching for an unspecified number of items comes in) 2). Sort merge (especially for linked lists).