# Data Structure - Common Sorting Methods

Stability: Two equal data, if the sorting algorithm ensures that its relative position does not change after sorting, we call it a stable sorting algorithm.

• ```  Example: 23(a) 143(b) after sorting is
```
• ```  The result is: 1 2 3(a) 3(b) 4 stable sorting
```
• ```  The result is: 1 2 3(b) 3(a) 4 unstable sorting
```

If a sort is stable, it can become unstable, but if a sort itself is unstable, you cannot make it stable.
There is usually no skip swap in a stable sort.

Seven commonly used comparison-based sorting

• ```    Insert Sort
```
• ```    Shell Sort
```
• ``` 	Select Sort
```
• ``` 	Heap Sorting
```
• ``` 	Bubble sort
```
• ``` 	Quick Sort (Important)
```
• ``` 	Merge Sort (Important)
```

Insert Sort Directly
Time Complexity:
Worst case: O (n^2) Best case (ordered case): O (n)
Very important: the more ordered the faster
Spatial Complexity: O(1)
Stability: stable sorting

```public static void insertSort(int[] array) {
for (int i = 1; i < array.length; i++) {
int t = array[i];
int j = i-1;
for (; j >= 0 ; j--) {
if (array[j] > t) {
array[j+1] = array[j];
}else {
array[j+1] = t;
break;
}
}
array[j+1] = t;
}
}
```

Shell Sort
Using the idea of grouping, inserting and sorting directly within a group
Time Complexity:
Worst case: O (n^2)
Best case: O(n)
Average: O (n^1.3)
Spatial Complexity: O(1)
Unstable sorting

```public static void shellSort(int[] array) {
int[] drr = {5,3,1};
for (int i:drr) {
shell(array,i);
}
}
public static void shell(int[] array, int gap) {
for (int i = gap; i < array.length; i++) {
int t = array[i];
int j = i - gap;
for (; j >= 0 ; j -= gap) {
if (array[j] > t) {
array[j+gap] = array[j];
}else {
array[j+gap] = t;
break;
}
}
array[j+gap] = t;
}
}
```

Select Sort
Time Complexity: O(n^2)
Spatial Complexity: O(1)
Unstable sorting

``` public static void selectSort(int[] array) {
for (int i = 0; i < array.length-1; i++) {
for (int j = i+1; j < array.length; j++) {
if (array[i] > array[j]) {
int t = array[i];
array[i] = array[j];
array[j] = t;
}
}
}
}
```

Heap Sorting
Time complexity: O(n*log(2)n)
Spatial Complexity: O(1)
Unstable sorting

```public static void adjustDown(int[] array, int root, int end) {
int p = root;
int c = p*2 + 1;
while(c < end) {
if (c+1<end && array[c]<array[c+1]) {
c++;
}
if (array[c] > array[p]) {
int t = array[c];
array[c] = array[p];
array[p] = t;
p = c;
c = 2*p+1;
}else {
break;
}
}
}

public static void createHeap(int[] array) {
for (int i = (array.length-1-1)/2; i >= 0; i--) {
}
}

public static void heapSort(int[] array) {
createHeap(array);
int end = array.length;
while (end > 0) {
int t = array[0];
array[0] = array[end-1];
array[end-1] = t;
end--;
}
}
```

Bubble sort
Time Complexity: O(n^2)
Spatial Complexity: O(1)
Stability ordering

```public static void bubbleSort(int[] array) {
for (int i = 0; i < array.length-1; i++) {
boolean flg = false;
for (int j = 0; j < array.length-1-i; j++) {
if (array[j] > array[j+1]) {
int t = array[j];
array[j] = array[j+1];
array[j+1] = t;
flg = true;
}
}
if (flg == false) {
return;
}
}
}
```

Quick Sort (Not Optimized) (Recursive)
Time Complexity: O(n*log(2)n) Worst case: O(n^2)
Spatial Complexity: O(log(2)n) Worst case: O(n)
Unstable sorting

```public static int partition(int[] array,int low,int high) {
int t = array[low];
while (high > low) {
while (array[high] >= t && high > low) {
high--;
}
array[low] = array[high];
while (array[low] <= t && high > low) {
low++;
}
array[high] = array[low];
}
array[high] = t;
return high;
}

public static void quick(int[] array, int left,int right) {
if (left >= right) {
return;
}
int par = partition(array,left,right);
quick(array,left,par-1);
quick(array,par+1,right);
}

public static void quickSort(int[] array) {
quick(array,0,array.length-1);
}
```

Merge Sort (recursive implementation)
Time complexity: n*log(2)n
Spatial Complexity: O(n)
Stable sorting

``` public static void mergeSort(int[] array) {
mergeSortInternal(array,0,array.length-1);
}

public static void mergeSortInternal(int[] array, int low, int high) {
if (low >= high) {
return;
}
//decompose
int mid = (low + high)>>>1;
mergeSortInternal(array, low, mid);
mergeSortInternal(array, mid+1, high);
//merge
merge(array,low, mid, high);
}

public static void merge (int[] array, int low, int mid, int high) {
int s1 = low;
int s2 = mid+1;
int len = high-low+1;
int[] ret = new int[len];
int i = 0;//Subscript used to represent ret array
//Corresponding location comparison
while (s1 <= mid && s2 <= high) {
if (array[s1] <= array[s2]) {
ret[i++] = array[s1++];
}else {
ret[i++] = array[s2++];
}
}

//Decide who hasn't finished and put the rest in the ret array
while (s1 <= mid) {
ret[i++] = array[s1++];
}

while (s2 <= high) {
ret[i++] = array[s2++];
}

//Put the number in the ret array in the array array
for (int j = 0; j < ret.length; j++) {
array[j+low] = ret[j];
}
}
```

Non-comparison based sorting

• Count Sort
• Cardinality sorting
• Bucket sorting

Only cardinality sorting is mentioned here
//Get maximum number

```public static int getMaxValue(int[] array) {
int maxValue = array[0];
for(int value : array) {
if (maxValue < value) {
maxValue = value;
}
}
return maxValue;
}

//Get the maximum number of digits
public static int getMaxLenght(int[] array) {
int maxValue = getMaxValue(array);
if (maxValue == 0) {
return 1;
}
int maxLenght = 0;
for (int i = maxValue; i != 0 ; i/=10) {
maxLenght++;
}
return maxLenght;
}

//Expand and coexist data
public static int[] arrayExpand(int[] array, int k) {
array = Arrays.copyOf(array, array.length+1);
array[array.length-1] = k;
return array;
}

//Cardinality sorting
public static void radixSort(int[] array) {
if (array.length == 0) {
System.out.println("The array is empty");
return;
}
int maxLenght = getMaxLenght(array);
int a = 10;
int b = 1;
for (int i = 0; i < maxLenght; i++) {
int[][] ret = new int[10][0];
for (int j = 0; j < array.length; j++) {
int k = array[j]%a/b;
ret[k] = arrayExpand(ret[k], array[j]);
}
int p = 0;
for (int[] arr :ret) {
for (int n :arr) {
array[p++] = n;
}
}
a *= 10;
b *= 10;
}
}
```

Tags: shell REST

Posted on Tue, 16 Jun 2020 12:39:28 -0400 by evlive