mirror of
https://github.com/hao14293/2021-Postgraduate-408.git
synced 2026-05-12 02:56:32 +08:00
Create shellSort.cpp
This commit is contained in:
21
Data-Structure/Sort/sorts/shellSort.cpp
Normal file
21
Data-Structure/Sort/sorts/shellSort.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
int a[] = {6, 5, 2, 8, 4, 1, 3, 7};
|
||||
int len = sizeof(a) / sizeof(a[0]);
|
||||
int step = len / 2; //初次增量为len/2
|
||||
while(step > 0){
|
||||
for(int i = step; i < len; i += step){
|
||||
while(i >= step && a[i - step] > a[i]){
|
||||
int temp = a[i - step];
|
||||
a[i - step] = a[i];
|
||||
a[i] = temp;
|
||||
i -= step;
|
||||
}
|
||||
}
|
||||
step = step / 2;
|
||||
}
|
||||
for(int i = 0; i < len; i++)
|
||||
cout << a[i];
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user