Insert sort directly divides the sequence to be sorted into two sequences: one sequence and one unordered sequence. During each sorting, take the first element of the unordered sequence, scan forward from the tail of the ordered sequence, compare the elements of the ordered sequence, and insert the element to the proper position of the ordered sequence, the ordered sequence continues to grow in order. The key code is as follows:
1. Insert the sorting header file: InsertSort. h
[Plain]
# Ifndef INSERTSORT_H
# Define INSERTSORT_H
Extern void InsertSort (int * pArr, int length );
# Endif
# Ifndef INSERTSORT_H
# Define INSERTSORT_H
Extern void InsertSort (int * pArr, int length );
# Endif
2. Insert the sorting source file: InsertSort. c
[Html]
# Include "InsertSort. h"
Void InsertSort (int * pArr, int length)
{
Int I, j, tmp;
For (I = 1; I <length; I ++)
{
J = I-1;
Tmp = * (pArr + I );
While (j> = 0 & tmp <* (pArr + j ))
{
* (PArr + j + 1) = * (pArr + j );
J --;
}
If (j! = I-1)
{
* (PArr + j + 1) = tmp;
}
}
}
# Include "InsertSort. h"
Void InsertSort (int * pArr, int length)
{
Int I, j, tmp;
For (I = 1; I <length; I ++)
{
J = I-1;
Tmp = * (pArr + I );
While (j> = 0 & tmp <* (pArr + j ))
{
* (PArr + j + 1) = * (pArr + j );
J --;
}
If (j! = I-1)
{
* (PArr + j + 1) = tmp;
}
}
}
3. main header file: main. h
[Cpp]
# Ifndef MAIN_H
# Define MAIN_H
# Include "InsertSort. h"
# Include <stdio. h>
Void outputArr (const int * pArr, const int length );
# Endif
# Ifndef MAIN_H
# Define MAIN_H
# Include "InsertSort. h"
# Include <stdio. h>
Void outputArr (const int * pArr, const int length );
# Endif
4. main source file: main. c
[Cpp]
# Include "main. h"
Int main (void)
{
Printf ("input array length: \ n ");
Int length;
Scanf ("% d", & length );
If (length <= 0)
{
Printf ("length must be larger 0 \ n ");
Return 1;
}
Int I;
Int arr [length];
For (I = 0; I <length; I ++)
{
Printf ("input arr [% d] value: \ n", I );
Scanf ("% d", & arr [I]);
}
Printf ("arr orig :");
OutputArr (arr, length );
InsertSort (arr, length );
Printf ("arr insert sort completed :");
OutputArr (arr, length );
}
Void outputArr (const int * pArr, const int length)
{
Int I;
For (I = 0; I <length; I ++)
{
Printf ("% d", * (pArr + I ));
}
Printf ("\ n ");
}
# Include "main. h"
Int main (void)
{
Printf ("input array length: \ n ");
Int length;
Scanf ("% d", & length );
If (length <= 0)
{
Printf ("length must be larger 0 \ n ");
Return 1;
}
Int I;
Int arr [length];
For (I = 0; I <length; I ++)
{
Printf ("input arr [% d] value: \ n", I );
Scanf ("% d", & arr [I]);
}
Printf ("arr orig :");
OutputArr (arr, length );
InsertSort (arr, length );
Printf ("arr insert sort completed :");
OutputArr (arr, length );
}
Void outputArr (const int * pArr, const int length)
{
Int I;
For (I = 0; I <length; I ++)
{
Printf ("% d", * (pArr + I ));
}
Printf ("\ n ");
}
4. Compile:
[Cpp]
[Root @ localhost insertSort] $ gcc-c InsertSort. c
[Root @ localhost insertSort] $ gcc-c main. c
[Root @ localhost insertSort] $ gcc-o main InsertSort. o main. o
[Root @ localhost insertSort] $ gcc-c InsertSort. c
[Root @ localhost insertSort] $ gcc-c main. c
[Root @ localhost insertSort] $ gcc-o main InsertSort. o main. o
If luck is not very bad, you will see the executable file main, execute main, roughly as follows:
[Cpp]
[Root @ localhost insertSort] $./main
Input array length:
5
Input arr [0] value:
43
Input arr [1] value:
65
Input arr [2] value:
76
Input arr [3] value:
1
Input arr [4] value:
43
Arr orig: 43 65 76 1 43
Arr insert sort completed: 1 43 43 65 76
[Root @ localhost insertSort] $./main
Input array length:
5
Input arr [0] value:
43
Input arr [1] value:
65
Input arr [2] value:
76
Input arr [3] value:
1
Input arr [4] value:
43
Arr orig: 43 65 76 1 43
Arr insert sort completed: 1 43 43 65 76