# Include <stdio. h>
// Insert sorting,
Void sortArray (int p [], int n );
Void insert (int p [], int a, int B );
// Select sorting
Void selectedSort (int p [], int n );
Void swap (int & p1, int & p2 );
// Predefined
# Define ArrayCounts 9
Int main (){
Int a [] = {1, 3, 8, 2, 6, 5, 7, 4, 9 };
SortArray (a, ArrayCounts );
For (int I = 0; I <ArrayCounts; I ++ ){
Printf ("% d", a [I]);
}
Printf ("\ n ");
Int B [] = {1, 3, 8, 2, 6, 5, 7, 4, 9 };
SelectedSort (B, ArrayCounts );
For (int I = 0; I <ArrayCounts; I ++ ){
Printf ("% d", B [I]);
}
Printf ("\ n ");
Return 0;
}
Void sortArray (int p [], int n ){
For (int I = 1; I <n; I ++ ){
For (int j = 0; j <I; j ++ ){
// Locate the location to be inserted
If (p [I] <p [j])
Insert (p, j, I );
}
}
}
// Insert function implementation
Void insert (int p [], int a, int B ){
Int temp = p [B];
Printf ("temp: % d \ n", temp );
While (a <B ){
P [B] = p [b-1];
B --;
}
P [a] = temp;
}
Void selectedSort (int p [], int n ){
Int index;
For (int I = 0; I <n; I ++ ){
// Find the subscript of the minimum value each time
Index = I;
For (int j = I + 1; j <n; j ++ ){
If (p [index]> p [j])
Index = j;
}
// Place the minimum value at the beginning
Swap (p [I], p [index]);
}
}
Void swap (int & p1, int & p2 ){
Int temp = p1;
P1 = p2;
P2 = temp;
}
This article is from the "Xiaoyu 2013" blog. For more information, contact the author!