#include <iostream> #include <string> #include <vector>using namespace std;void swap (int a[], int i, int j); void Insert_sort (int a[], int n) {for (int i = 1; i < n; i++) {int temp = A[i], j = i;while (J&&temp < A[j -1]) {a[j] = a[j-1];j--;} A[J] = temp;}} void Shell_sort (int a[], int n) {for (int inc. = N/2; Inc; Inc/= 2) {for (int i = inc; I < N; i++) {int temp = a[i],j=i; while (J>=inc&&temp < A[j-inc]) {A[j] = a[j-inc];j-= Inc;} A[J] = temp;}}} void Select_sort (int a[], int n) {for (int i = 0; i < n; i++) {int index = I;int temp = a[i];for (int j = i+1; J < N; J + +) {if (A[j] < A[index]) {index = j;}} A[i] = A[index];a[index] = temp;}} void Bubble_sort (int a[], int n) {/*for (int i = n; i > 0; i--) {for (int j = 1; j < I; J + +) {if (A[j] < a[j-1]) {s WAP (A, J, j-1);}}} */for (int i = 0; i < n; i++) {for (int j = n-1; j>i; j--) {if (A[j] < a[j-1]) {Swap (A, J, j-1);}}}} void merge (int a[], int l, int m, int r) {int *temp = NEW Int[r-l];int i = 0, j = l, K = M;while (J < m&&k < r) {if (A[j] < a[k]) {temp[i++] = a[j++];} else{temp[i++] = a[k++];}} while (j! = m) {temp[i++] = a[j++];} while (k! = r) {temp[i++] = a[k++];} for (int i = l; i < R; i++) {a[i] = temp[i-l];} delete[] temp;temp = nullptr;} void Merge_sort (int a[], int l, int r) {if (L + 1 >= r) {return;} int middle = (L + R)/2;merge_sort (A, L, middle), Merge_sort (A, middle, R), merge (A, L, Middle, r);} void Quick_sort (int a[], int l, int r) {if (L + 1 >= r) {return;} int pivot = A[l];int i = l, j = R;while (true) {do{i++;} while (A[i] < pivot&&i<r);d o{j--;} while (A[j] >= Pivot&&j > L); if (i >= j) Break;swap (A, I, j);} A[L] = A[j];a[j] = Pivot;quick_sort (A, L, j); Quick_sort (A, j + 1, r);} void Shut_down (int a[], int hole,int n) {while (Hole < n) {int firstindex = hole * 2 + 1;if (FirstIndex + 1 < N&&A Mp;a[firstindex] < A[firstindex + 1]) {firstindex++;} if (FirstIndex < N&&a[hole] < A[firsTindex]) {swap (A, hole, FirstIndex);} Hole = FirstIndex;}} void make_heap (int a[], int n) {for (int i = (n-1)/2; i>=0; i--) {Shut_down (A, I, n);}} void Heap_sort (int a[], int n) {make_heap (A, N), for (int i = n-1; i; i--) {swap (a, 0, i); Make_heap (A, I);}} void swap (int a[], int i, int j) {int temp = A[i];a[i] = a[j];a[j] = temp;} void print (int a[], int n) {for (int i = 0; i < n; i++) {cout << a[i] << "";} cout << Endl;} /*int Main () {int* a = new Int[10];for (int i = 0; i <; i++) {A[i] = rand ()% 10;} Print (A, ten),//insert_sort (A, ten),//shell_sort (A, ten),//select_sort (A, ten),//bubble_sort (A, ten);//merge_sort (a,0, 10 );//quick_sort (A, 0, ten);//heap_sort (A, ten);//print (A, 10);} */
Interview questions---various sorts. Insertion, selection, bubbling, shell, heap, quick row