# Data Structure (II) - 03 Sorting
> PART (II) - 03
> Other parts in this series $\rightarrow$ [Data Structure - Syllabus ](/bU7575BKTwmwkvmJ5742xA)
# Sorting Algorithms
A Sorting Algorithm is used to rearrange a given array or list of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of elements in the respective data structure.
> ***Sorting Terminology***
> * **In-place Sorting**: An in-place sorting algorithm uses constant space for producing the output (modifies the given array only) or copying elements to a temporary storage. Examples: Selection Sort, Bubble Sort Insertion Sort and Heap Sort.
> * **Internal Sorting**: Internal Sorting is when all the data is placed in the main memory or internal memory. In internal sorting, the problem cannot take input beyond its size. Example: heap sort, bubble sort, selection sort, quick sort, shell sort, insertion sort.
> * **External Sorting**: External Sorting is when all the data that needs to be sorted cannot be placed in memory at a time, the sorting is called external sorting. External Sorting is used for the massive amount of data. Examples: Merge sort, Tag sort, Polyphase sort, Four tape sort, External radix sort, etc.
> * **Stable sorting**: When two same items appear in the same order in sorted data as in the original array called stable sort. Examples: Merge Sort, Insertion Sort, Bubble Sort.
> * **Unstable sorting**: When two same data appear in the different order in sorted data it is called unstable sort. Examples: Selection Sort, Quick Sort, Heap Sort, Shell Sort.
## selection sort

supose size=$n$
1. start by index $i$=0
2. find the minimum element's index $min\_index$ between index $i$ to index $n$-1 **(select the minimum)**
3. swap the $min\_index$ and $i$
4. $i$ plus 1
5. repeat step 2 to 4 when $i<n$
```cpp=
void selection_sort(int arr[],int size){
for(int i=0;i<size;i++){
int min_index=i;
for(int j=i;j<size;j++){
if(arr[j]<arr[min_index]){
min_index=j;
}
}
swap(arr[i],arr[min_index]);
}
}
```
## insertion sort

supose size=$n$
1. start by index $i$=1 and set $current\_index$=$i$
2. if previous number($current\_index$-1) is bigger than number at $current\_index$
3. swap $current\_index$ and $current\_index$-1
4. $current\_index$ minus 1
5. repeat step 2 to 4 until $current\_index$ is bigger than privious one. **(insert to the correct place)**
6. $i$ plus 1
7. repeat step 5 and 6 when $i<n$
```cpp=
void insertion_sort(int arr[],int size){
for(int i=1;i<size;i++){
int current_index=i;
while(arr[current_index-1]>arr[current_index] && current_index>0){
swap(arr[current_index-1],arr[current_index]);
current_index--;
}
}
}
```
## heap sort
:::warning
Prerequisite knowledge$\rightarrow$ [heap](https://hackmd.io/@benny-liu/HyMeY-3T0)
:::

1. create max heap
2. Remove largest node (move to sorted partition)
3. heapify
4. repeat step 2 and 3 until heap is empty
```cpp=
void build_max_heap(int* arr, int size, int parent){
if(parent<0) return; //stop after parent is root
int left=parent*2+1, right=parent*2+2;
if(arr[parent]<arr[left] && (arr[left]>arr[right]||right>=size) && left<size){
swap(arr[parent],arr[left]);
build_max_heap(arr,size,left);//check if left subtree is still corrected after swapping
}else if(arr[parent]<arr[right] && right<size){
swap(arr[parent],arr[right]);
build_max_heap(arr,size,right);//check if right subtree is still corrected after swapping
}
build_max_heap(arr,size,parent-1);//go to next subtree
}
void heapify(int* arr, int size, int parent){
int left=parent*2+1, right=parent*2+2;
if(arr[parent]<arr[left] && (arr[left]>arr[right]||right>=size) && left<size){
swap(arr[parent],arr[left]);
heapify(arr,size,left);//check if left subtree is still corrected after swapping
}else if(arr[parent]<arr[right] && right<size){
swap(arr[parent],arr[right]);
heapify(arr,size,right);//check if right subtree is still corrected after swapping
}
}
void heap_sort(int arr[],int size){
build_max_heap(arr,size, (size-1)/2);
while(size>0){
swap(arr[0],arr[size-1]);
size--;
heapify(arr,size,0);
}
}
```
## merge sort

1. Divide the list or array recursively into two halves until it can no more be divided.
1. Each subarray is sorted individually using the merge sort algorithm.
1. The sorted subarrays are merged back together in sorted order. The process continues until all elements from both subarrays have been merged.
```cpp=
void merge_sort(int arr[],int size){
if(size<=1) return;
int left_array[size/2],right_array[size-size/2];
for(int i=0;i<size/2;i++) left_array[i]=arr[i];
for(int i=0;i<size-size/2;i++) right_array[i]=arr[i+size/2];
merge_sort(left_array,size/2);
merge_sort(right_array,size-size/2);
int l_index=0,r_index=0;
for(int i=0;i<size;i++){
if((l_index<size/2&&left_array[l_index]<=right_array[r_index])||r_index>=size-size/2){
arr[i]=left_array[l_index];
l_index++;
}else{
arr[i]=right_array[r_index];
r_index++;
}
}
}
```
## quick sort

1. Using first element as dividing point
2. For other element. If bigger, throw to bigger_array. If smaller, throw to smaller_array.
3. array = smaller_array + dividing point + bigger_array
1. do the step 2 to 3 to the smaller_array and bigger_array
2. If array size equals 1 or smaller, stop
```cpp=
void quick_sort(int arr[],int size){
if(size<=1) return;
int smaller_array[size],bigger_array[size],bigger_size=0,smaller_size=0,cur=arr[0];
for(int i=1;i<size;i++){
if(arr[i]>cur){
bigger_array[bigger_size]=arr[i];
bigger_size++;
}else{
smaller_array[smaller_size]=arr[i];
smaller_size++;
}
}
quick_sort(smaller_array,smaller_size);
quick_sort(bigger_array,bigger_size);
for(int i=0;i<smaller_size;i++) arr[i]=smaller_array[i];
arr[smaller_size]=cur;
for(int i=0;i<bigger_size;i++) arr[i+smaller_size+1]=bigger_array[i];
}
```
## radix sort

1. set 10 bucket(for number 0 to 9).
2. check the last digit of each element
3. distribute them into buckets by the digit number we got.
4. put them back to array by the order of buckets(0 to 9)
5. check the next digit.
6. repeat step 3 to 5 until the highest digit have been reached.
```cpp=
void radix_sort(int arr[], int size){
int digit=1,bucket_size[10]={0,0,0,0,0,0,0,0,0,0};
while(bucket_size[0]!=size){
int buckets[10][size], count=0;
for(int i=0;i<10;i++) bucket_size[i]=0;
for(int i=0;i<size;i++){
buckets[(arr[i]/digit)%10][bucket_size[(arr[i]/digit)%10]]=arr[i];
bucket_size[(arr[i]/digit)%10]++;
}
for(int i=0;i<10;i++){
for(int j=0;j<bucket_size[i];j++){
arr[count]=buckets[i][j];
count++;
}
}
digit*=10;
}
}
```
# sort in Library
## qsort( ) function in C library stdlib.h or cstdlib
```cpp=
int compare(const void* a, const void* b){//compare function for qsort
return (*(int*)a - *(int*)b);
}
```
```cpp=
qsort(array, size, sizeof(int), compare);
```
## sort( ) function in C++ library (STL)
```cpp=
sort(array,array+size);
```