Direct insert sorting (recursive and non-Recursive Implementation methods), direct insert Recursion

Source: Internet
Author: User

Direct insert sorting (recursive and non-Recursive Implementation methods), direct insert Recursion

An array has n elements. If the first n-1 elements have been sorted, insert the n-th element to the first n-1 elements so that the arrays are sorted in an orderly manner.

As for how the n-1 elements have been sorted first, we can assume that the previous N-2 elements have been sorted, insert the n-1 elements to the previous N-2 element.

And so on until only one element is left, that is, the first element. Sorting is completed.


The Code is as follows:

#include<iostream>using namespace std;void Insert_Sort(int *A,int n){int i,j,a;for(i=0;i<n-1;i++){j=i+1;a=A[j]; while(a<A[j-1]){  A[j]=A[j-1];  j--;}A[j]=a;}}void main(){int A[4]={21,2,34,1};Insert_Sort(A,4);for (int i=0;i<4;i++){cout<<A[i]<<endl;}system("pause");}

Write the recursive version

# Include <iostream> using namespace std; // Insert n into the preceding sorted array void Insert (int * a, int n) {int I = n-1; int key = a [n]; while (I> = 0) & key <a [I]) {a [I + 1] = a [I]; I --;} a [I + 1] = key;} void Insert_Sort (int * A, int n) {if (n> 0) {Insert_Sort (A, n-1 ); // recursion indicates that Insert (A, n) has been sorted before;} else // the exit of recursion is n = 0 return;} void main () {int A [4] = {21,2, 34,1}; Insert_Sort (A, 3); for (int I = 0; I <4; I ++) {cout <A [I] <endl;} system ("pause ");}

Time Complexity Analysis:

1. In the best case, the raw data has been sorted, so the time complexity is O (N)

2. In the worst case, the original array is sorted in reverse order, which is O (n²)

3. In average condition, O (n²)


The closer the element data is to order, the higher the efficiency of direct insertion.


The C language uses Recursion to implement insertion sorting, select sorting, and quickly sort. the Merge Sorting Algorithm should be noted.

// InsertionSort
Void insertionSort (int a [], int size ){
Int I, j, key;

For (I = 0; I <size; I ++ ){
Key = a [I];
J = I-1;
While (j> = 0 & key <a [j]) {// Insert the element to the previously ordered tuples.
A [j + 1] = a [j];
J --;
}
A [j + 1] = key;
}
}

// MergeSort
Void merge (int a [], int p, int q, int r) {// merge two child groups
Int I, j, k, n1, n2;
Int * array1, * array2;
N1 = q-p + 1,
N2 = r-q;

Array1 = (int *) calloc (n1 + 1, sizeof (int ));
Array2 = (int *) calloc (n2 + 1, sizeof (int ));
If (array1 = NULL | array2 = NULL ){
Printf ("Error: calloc failed in concat \ n ");
Exit (EXIT_FAILURE );
}
For (I = 0; I <n1; I ++)
Array1 [I] = a [p + I];
For (I = 0; I <n2; I ++)
Array2 [I] = a [q + 1 + I];
Array1 [n1] = MAXNUMBER;
Array2 [n2] = MAXNUMBER;
I = 0, j = 0;
For (k = p; k <= r; k ++)
If (array1 [I] <= array2 [j])
A [k] = array1 [I ++];
Else
A [k] = array2 [j ++];
Free (array1 );
Free (array2 );
}

Void mergeSort (int a [], int p, int r) {// recursive call of merging
Int q;
If (p <r ){
Q = (p + r)/2;
MergeSort (a, p, q );
MergeSort (a, q + 1, r );
Merge (a, p, q, r );
}
}

// QuickSort
Int partition (int a [], int p, int r) {// grouping Function
Int I, j, x, temp;
X = a [r];
I = p-1;

For (j = p; j <r; j ++)
If (x> a [j]) {
Temp = a [++ I];
A [I] = a [j];
A [j] = temp;
}
Temp = a [++ I];
A [I] = a [r];
A [r] = temp;

Return I;
}

Void quickSort (int a [], int p, int r) {// fast sorting
Int q;
If (p <r ){
Q = partition (a, p,... the remaining full text>

Insert and sort by recursion

Program STU_Insertion_Sort;
Uses crt;
Const n = 10;
Type sz = array [1 .. n] of integer;
Var a: sz;
I: integer;
Procedure sort (var a: sz );
Var I, j, key: integer;
Begin
For j: = 2 to n do begin
Key: = a [j];
I: = J-1;
While (I> 0) and (a [I]> key) do begin
A [I + 1]: = a [I];
Dec (I );
End;
A [I + 1]: = key;
End;
End;

Begin
WriteLn ('Hi! ');
For I: = 1 to n do
Readln (a [I]);
Sort ();
For I: = 1 to n do
Writeln (a [I]);
Readkey;
End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.