mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Shell Sort
This commit is contained in:
parent
eeddc8af8a
commit
7f4f03aa40
45
ShellSort.cpp
Normal file
45
ShellSort.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int size=10;
|
||||
int array[size];
|
||||
// Input
|
||||
cout<<"\nHow many numbers do want to enter in unsorted array : ";
|
||||
cin>>size;
|
||||
cout<<"\nEnter the numbers for unsorted array : ";
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
cin>>array[i];
|
||||
}
|
||||
|
||||
// Sorting
|
||||
for (int i = size/2; i>0 ; i=i/2)
|
||||
{
|
||||
for (int j = i; j <size ; j++)
|
||||
{
|
||||
for (int k = j-i; k>=0; k=k-i)
|
||||
{
|
||||
if (array[k]<array[k+i])
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
int temp=array[k+i];
|
||||
array[k+i]=array[k];
|
||||
array[k]=temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
cout<<"\nSorted array : ";
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
cout<<array[i]<<"\t";
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user