mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-02 10:12:45 +08:00
Sorting
This commit is contained in:
38
Sorting/Insertion Sort.cpp
Normal file
38
Sorting/Insertion Sort.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
//Insertion Sort
|
||||
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int Array[n];
|
||||
cout<<"\nEnter any 6 Numbers for Unsorted Array : ";
|
||||
|
||||
//Input
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
cin>>Array[i];
|
||||
}
|
||||
|
||||
//Sorting
|
||||
for(int i=1; i<n; i++)
|
||||
{
|
||||
int temp=Array[i];
|
||||
int j=i-1;
|
||||
while(j>=0 && temp<Array[j])
|
||||
{
|
||||
Array[j+1]=Array[j];
|
||||
j--;
|
||||
}
|
||||
Array[j+1]=temp;
|
||||
}
|
||||
|
||||
//Output
|
||||
cout<<"\nSorted Array : ";
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
cout<<Array[i]<<"\t";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user