# [Java] select sort & heap sort

### 1 select sort

1.1 basic ideas

Each time, the smallest (or largest) element is directly selected from the data elements to be sorted and stored at the beginning of the sequence until all the data elements to be sorted are arranged.

1.2 algorithm steps

1) find the largest (smallest) element in the unsorted sequence

2) if it is not the last (first) element of this group, exchange it with the last (first) element of this group

3) repeat the above steps until one element remains in the collection

1.3 diagram

1.4 code implementation

```public static void StraightSelectSort(int[] arr,int size){
for(int i=0;i<size;i++){
/*Disorder [size-i,size-1]
Orderly [0,size-1-i]*/
int maxId = 0;
//Find the maximum element subscript of the entire unordered interval
for(int j=0;j<size-i;j++){
if(arr[maxId] < arr[j]){
maxId=j;
}
}
/*maxId Subscript of the largest element in the record unordered interval
Exchange the maxId element with the last element of the unordered interval */
swap(arr,maxId,size-i-1);
}
}

public static void swap(int[] arr, int i,int j) {
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}```

1.5 optimization (select two numbers at a time)

```public static void SelectSortOP(int[] arr,int size){
int begin = 0;
int end = size-1;
/*[0,begin-1]  Orderly
[begin,end]  disorder
[end+1,size-1]  Orderly */
while(begin<=end) {
int max = begin;
int min = begin;
for (int i = begin; i <= end; i++) {
if (arr[i] >= arr[max]) {
max = i;
}
if (arr[i] <= arr[min]) {
min = i;
}
}
/*The largest element is placed at the end of the unordered range
The smallest element is placed at the beginning of the unordered range*/
swap(arr, begin, min);
if(begin == max){
max = min;
}
swap(arr, end, max);
begin++;
end--;
}
}

public static void swap(int[] arr, int i,int j) {
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}```

### 2 heap sort

2.1 basic ideas

Heap sorting is a sort algorithm designed by using the data structure of heap tree (heap). It is a kind of selective sorting, which selects data by heap.

Note: arrange ascending order to build a large pile, and arrange descending order to build a small pile

2.2 algorithm steps

1) heap building: from the last non leaf node to 0, continuously downward adjustment

2) exchange head (maximum) and tail

3) reduce the heap size by 1, and call Heapify() to adjust the unordered part

4) repeat steps 2) and 3) until the heap size is 1

2.3 diagram

2.4 code implementation

```public static void HeapSort(int[] arr,int size){
//Build a pile, in ascending order
CreateHeap(arr,size);
/*Process one element at a time
Disorder [0,size-i-1]
Orderly [size-i,size-1]*/
for(int i=0;i<size;i++){
//Exchange the largest element with the last element of the unordered interval
swap(arr,0,size-1-i);
//The nature of the heap is destroyed (only the root), and the length of the disordered part is adjusted to size-i-1
//Heapify(arr, size-i-1, 0);
HeapifyNoR(arr,size-i-1,0);
}
}

public static void CreateHeap(int[] arr,int size){
//From the last non leaf node, all the way to 0, continuously downward adjustment
for(int i=(size-2)/2; i>=0; i--){
//Heapify(arr,size,i);
HeapifyNoR(arr,size,i);
}
}

public static void Heapify(int[] arr,int size,int index){
int left = 2*index+1;
int right = 2*index+2;
if(left>=size){
return;
}
int max = 0;
if(right<size && arr[right]>arr[left]){
max=right;
}else {
max = left;
}
if(arr[index]>=arr[max]){
return;
}
swap(arr,index,max);
Heapify(arr,size,max);
}

public static void HeapifyNoR(int[] arr,int size,int index){
//Judge whether it is a leaf node
while(index*2+1<size) {
//It's not a leaf node
int left = index*2+1;
int right = index*2+2;
int max = 0;
//Right child and right child > left child
if(right<size && arr[left]<arr[right]){
max = right;
}else {
//1) no right child
//2) have right child and left child > right child
max = left;
}
//The oldest child is compared with the root. If the root is small, exchange
if (arr[max] > arr[index]) {
swap(arr, max, index);
}
//Replace index value and continue to adjust
index = max;
}
}

public static void swap(int[] arr, int i,int j) {
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}```

Posted on Thu, 07 Nov 2019 13:20:59 -0500 by Tyco