Because the teacher compared to the second-round interview more like to ask sorting algorithm that, so recently put the sorting algorithm review again, by the way write code.
1 Direct Insert sort.
#include <stdio.h>void inssort (int r[], int n) {int I, J, temp;for (i = 1; i < n; i++) {temp = R[i];j = I-1;while ( Temp < R[J])//Find the position with insert {r[j+1] = R[j];j = j-1;} R[J+1] = temp; Inserts a numeric value into the sorted queue}}void print (int r[], int n) {int i;for (i = 0; i < n; i++) printf ("%d", R[i]);p UTS ("\ n");} int main () {int a[10] = {3, 1, 5, 2, 7, 8, 9, 4, 6, 0};p rintf ("before sort:\n");p rint (A, ten), Inssort (A, ten);p rintf ("Sort AF ter:\n ");p rint (A, ten); return 0;}
As the above code: the TEMP variable is used to place the band insertion value.
The spatial complexity of the direct insertion algorithm O (1), because only 1 variables were used
The spatial complexity depends on the number of columns to be sorted. Best case Move Times 2 * (n-1), worst case Move (n+4) * (n-1)/2, average complexity n^2/4, so time complexity bit 0 (n^2)
Relive sorting algorithms