#include<iostream> using namespace std;
#define Max_ 10
void InsertSort(int arr[], int nLength, int nDelt = 1); //for Straight && Shellvoid Show(int arr[], int nLength) { for (int i = 0; i < nLength; i ++) { printf("%d ", arr[i]); } printf("\n"); }
void InsertSort(int arr[], int nLength, int nDelt /*= 1*/) { for (int i = nDelt; i < nLength; i++) { if (arr[i] < arr[i - nDelt]) { int nTemp = arr[i]; arr[i] = arr[i - nDelt]; int j = i - nDelt; while(nTemp < arr[j]) //if j = -1 , VS is right ? { arr[j + nDelt] = arr[j]; j -= nDelt; } arr[j + nDelt] = nTemp; } } }
void ShellSort(int arr[], int nLength) { int nDelt = nLength /2; while (nDelt >= 1) { InsertSort(arr, nLength, nDelt); nDelt /= 2; } }
int main() { int arr_test[Max_] = {877, 477, 252, 22233, 5554, 155, 644, 932, 77, 27}; Show(arr_test, Max_); InsertSort(arr_test, Max_); Show(arr_test, Max_);
// //int nLength = 0; //int nInput = 0; //printf("Please input the length of array: "); //scanf("%d",&nLength); //for (int i = 0; i < nLength; i++) //{ // printf("Please input element %d : ", i + 1); // fflush(stdin); // scanf("%d", &nInput); // arr_test[i] = nInput; //} //Show(arr_test, Max_); //InsertSort(arr_test, Max_); //Show(arr_test, Max_); //
int arr_test1[Max_] = {877, 477, 252, 22233, 5554, 155, 644, 932, 77, 27}; Show(arr_test1, Max_); ShellSort(arr_test1, Max_); Show(arr_test1, Max_);
system("pause"); return 0; }