No template or the like. All int-type subscripts start from 0 and start from 1.
1. Insert
[Cpp]
# Include <iostream>
# Include <cstdio>
# Define N 1005
Using namespace std;
Int n, a [N];
Void InsertSort (int a [], int n) // subscript starts from 0
{
Int I, j, temp;
For (I = 1; I <n; I ++)
{
Temp = a [I];
For (j = I; j> 0 & temp <a [j-1]; j --) a [j] = a [j-1];
A [j] = temp;
}
}
Int main ()
{
Int I;
Scanf ("% d", & n );
For (I = 0; I <n; I ++) scanf ("% d", & a [I]);
InsertSort (a, n );
For (I = 0; I <n; I ++) printf ("% d \ n", a [I]);
Return 0;
}
2. Select
[Cpp]
# Include <iostream>
Using namespace std;
Int a [105];
// Subscript starts from 1
Void SelectSort (int a [], int n)
{
Int I, j, pos;
For (I = 1; I <n; I ++)
{
Pos = I;
For (j = I; j <= n; j ++)
{
If (a [j] <a [pos])
Pos = j;
}
Int temp = a [pos];
A [pos] = a [I];
A [I] = temp;
}
}
Int main ()
{
Int n, I;
Cin> n;
For (I = 1; I <= n; I ++) cin> a [I];
SelectSort (a, n );
For (I = 1; I <= n; I ++)
Cout <a [I] <"";
Cout <endl;
Return 0;
}
3. Bubble
[Cpp]
# Include <iostream>
Using namespace std;
Int a [105];
// Subscript starts from 1
Void BubbleSort (int a [], int n)
{
Int I, j;
For (I = 1; I <n; I ++)
{
For (j = 1; j <= n-I; j ++)
{
If (a [j]> a [j + 1])
{
Int temp = a [j + 1];
A [j + 1] = a [j];
A [j] = temp;
}
}
}
}
Int main ()
{
Int n, I;
Cin> n;
For (I = 1; I <= n; I ++)
Cin> a [I];
BubbleSort (a, n );
For (I = 1; I <= n; I ++)
Cout <a [I] <"";
Cout <endl;
Return 0;
}
4. Fast sorting
[Cpp]
# Include <iostream>
Using namespace std;
Int n;
Int a [105];
// Subscript starts from 0
Int partition (int a [], int low, int high)
{// A trip in quick sorting
Int key; // used as the pivot
Key = a [low];
While (low
{
While (low -- High;
A [low] = a [high];
While (low ++ Low;
A [high] = a [low];
}
A [low] = key;
Return low;
}
Void qsort (int a [], int low, int high)
{// Recursive form of quick sorting
Int loc;
If (low
{
Loc = partition (a, low, high); // call the sorting result
Qsort (a, low, loc-1 );
Qsort (a, loc + 1, high );
}
}
Int main ()
{
Int I;
Cin> n;
For (I = 0; I <n; I ++) cin> a [I];
Qsort (a, 0, n-1 );
For (I = 0; I <n; I ++) cout <a [I] <"";
Cout <endl;
Return 0;
}
5. Merge
[Cpp]
# Include <iostream>
# Define N 105
Using namespace std;
Int n;
Int a [N], t [N];
/* Merge */
// Subscript starts from 0
Void Merge (int a [], int l, int m, int r)
{
/* P points to the output range */
Int p = 0;
/* I and j point to two input intervals */
Int I = l, j = m + 1;
/* When neither of the two input ranges is null */
While (I <= m & j <= r)
{/* Transfer a record with a small keyword to the output interval */
If (a [I]> a [j])
T [p ++] = a [j ++];
Else
T [p ++] = a [I ++];
}
/* Transfer non-empty input intervals to the output intervals */
While (I <= m) t [p ++] = a [I ++];
While (j <= r) t [p ++] = a [j ++];
/* Copy the result to the original input array after merging */
For (I = 0; I <p; I ++)
A [l + I] = t [I];
}
/* Merge and sort */
Void MergeSort (int a [], int l, int r)
{
Int m;
If (l <r)
{/* Divide an input sequence whose length is n into two subsequences whose length is n/2 */
M = (l + r)/2;
/* Merge and sort two subsequences respectively */
MergeSort (a, l, m );
MergeSort (a, m + 1, r );
/* Merge two sorted subsequences into the final ordered sequence */
Merge (a, l, m, r );
}
}
Int main ()
{
Int I;
Cin> n;
For (I = 0; I <n; I ++) cin> a [I];
MergeSort (a, 0, n-1 );
For (I = 0; I <n; I ++) cout <a [I] <"";
Cout <endl;
Return 0;
}
6. Heap
[Cpp]
# Include <iostream>
Using namespace std;
Int n;
Int a [105];
// Subscript starts from 1
Void HeapAdjust (int A [], int a, int z)
{
Int I, j;
For (I = a, j = 2 * I; j <= z; I = j, j = 2 * I)
{// I is the parent and j is the child
If (j <z & A [j + 1]> A [j]) j ++; // the Dading heap goes down along the big child
If (A [I] <A [j])
Swap (A [I], A [j]);
Else break;
}
}
Void HeapSort (int A [], int n)
{
Int I;
For (I = n/2; I> = 1; I --) // the first non-leaf node from the bottom up.
HeapAdjust (A, I, n );
For (I = n; I> 1; I --)
{// Swap A [1] with the final element A [I] (I = n,..., 1), then heap
Swap (A [1], A [I]);
HeapAdjust (A, 1, I-1 );
}
}
Int main ()
{
Int I;
Cin> n;
For (I = 1; I <= n; I ++)
Cin> a [I];
HeapSort (a, n );
For (I = 1; I <= n; I ++) cout <a [I] <"";
Cout <endl;
Return 0;
}
Author: sdj222555