Sorting an array algorithm in C and C++
| Previous |< Basic data structures |
Next Binary search>| |
Sorting an array in non-descending or non-increasing order involves finding one permutation of the elements of the array in which the elements appear in non-descending order, i.e. non-increasing order.
Sort an array by selection method
Let's assume that we have a set of numbers:int A [7] = {2, 5, 11, 4, 1, 9, 0};
To sort it in, for example, By decreasing the order from the largest to the smallest, we will do the following:
We pass through all the members of the series and find the greatest of all and put it first.
int i=0;
int for(int j = i+1; j < n; j++){
int for(int j = i+1; j < n; j++){
if(A[j] > A[i])
{
}{
/*Swap two elements*/
int b=A[i];
A[i]=A[j];
A[j]=b;
/*End of swap*/
}int b=A[i];
A[i]=A[j];
A[j]=b;
/*End of swap*/
Figure 3: Sorting array algorithm-Selection Sort-finish
Finally, the entire sort code:for(int i=0; i<N-1; i++)
{
for(int j=i+1; j<N; j++)
{
if(A[ j ] < A [ i ] )
{
int b=A[ i ];
A[ i ]=A[ j ];
A[ j ]=b;
}
}
}
Example: Load the number of n array elements, then set a series of randomly generated n integers whose value is 0 to 100. Sort the array in descending order and print on the screen.
#include <iostream>#include<stdlib.h>
#define DIM 20
using namespace std;
int main()
{
int a[DIM], n, j;
cin>>n;
if(n<=0 || n>DIM)return 1;
//Loading elements of a array
cout<<"Array:"<<endl;
for (int i=0; i<n; i++){
//cin>>a[i];//4,2,8,9,1,5,6
a[i]=rand()%100;
cout<<a[ i ]<<" ";
}
cout<<endl;
//Sorting array
for(int i=0;i<n-1;i++){
for(j=i+1; j<n; j++){
if(a[ j ] > a[ i ]){
//Replacement
int b=a[i];
a[i]=a[j];
a[j]=b;
}
}
}
cout<<"Sorted array:"<<endl;
for (int i=0; i<n; i++){
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
Sorting arrays by insertion method
Figure 4: Algorithm for sorting by insertion methodIf an array (An) is assigned to elements of someone, arranged type T, which needs to be arranged in a non-ordering order, this sorting method starts from the assumption that we have arranged the initial part of the array, (this certainly applies to i = 2, since the sub-array with one element arranged) and in each step, beginning with i = 2 and increasing i, the i-th element is placed in the right place in relation to the first (arranged) i - 1.
Firstly, the currently examined element is inclined to auxiliary variable b. Thereafter, a consecutive comparison of the values aj and b for j = i-1, i-2, ..., 0 is performed. Each time it is determined that aj > b, the element aj is moved to the place aj + j. The cycle of comparing and moving ends when the first time it is not necessary to move or when it becomes less than zero.
Finally, it is still necessary to put the tested value from the variable b into its place, i.e. u element aj + 1.
Algorithm for sorting by insertion method
Figure 5: Algorithm for sorting by insertion method
#include <iostream>
#define DIM 10
using namespace std;
/*Sorting by insertion method*/
int main()
{
int a[DIM], n, j;
cin>>n;
if(n<=0 || n>DIM) return 1;
//Reading elements of a array
for (int i=0;i<n;i++){
cin>>a[i]; //4,2,8,9,1,5,6
}
//Sorting array
for(int i=1;i<n;i++){
int b=a[i];
for(j=i-1;j>=0;j--){
if(a[j]>b){
a[j+1]=a[j];
}
else break;
}
a[j+1]=b;
}
cout<<"Sorted array:"<<endl;
for (int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
Merge sort
Quick-Sort
| The sorting algorithm that proved to be the fastest. It consists of a recursive repetition of the separation of the elements of an array around some value that has been adopted as "middle value". This is advanced technique of sorting an array. Read more at webpage: Quick-Sort |
![]() |
| Previous |< Basic data structures |
Next Binary search>| |




