# Sorting algorithm (2): exchange sorting

Keywords: Java less

###### 1. Bubble sorting

Principle: compare the adjacent two numbers, and exchange them from small to large. After one pass, the largest (smallest) number will be exchanged to the last one, and then compare the two numbers from the beginning until the last i.

###### Code implementation:
```for(int i=0;i<a.length-1;i++){
for(int j=i + 1; j<a.length;j++){
if(a[j] < a[j-1]){
a[j] = a[j] ^ a[j-1];
a[j-1] = a[j] ^ a[j-1];
a[j] = a[j] ^ a[j-1];
}
}
}```

Analysis: stability, space complexity O(1), time complexity [the best, worst, average are O(n*n)].

###### Code implementation:
```for(int i=0;i<a.length-1;i++){
bool swap = false; //Define a flag variable swap
for(int j=1;j<a.length-i;j++){
if(a[j] < a[j-1]){
a[j] = a[j] ^ a[j-1];
a[j-1] = a[j] ^ a[j-1];
a[j] = a[j] ^ a[j-1];
swap = true;
}
}
if(!swap){return;}
}```

Analysis: stability, space complexity O(1), time complexity [the best is O(n), the worst, the average is O(n*n)].

###### 2. Fast sorting (improvement of bubble sorting)

Principle: the data to be sorted is divided into two independent parts by one sorting, and all the data in one part is smaller than that in the other part. Then according to this method, the two parts of data can be sorted quickly, and the whole sorting process can be recursive.

Implementation ideas:

1. Take the first key k1 as the control word (pivot), divide [k1,k2,....kn] into two sub areas. Make all the keywords in the left area less than or equal to the control word, and all the keywords in the right area greater than or equal to the control word. Finally, the control word occupies the appropriate position between the two sub areas.
2. Recurse the left and right regions respectively.

Code implementation:

```public static void main(String[] args) {
int[] arr = new int[]{50,10,90,20,40,60,80,70};
quickSort(arr,0,arr.length-1);
print(arr);
}```
```private static void print(int[] arr) {
for (int i : arr){
System.out.println(i + " ");
}
}
private static void quickSort(int[] arr, int left, int right) {
//Define keywords pivot
int pivot = 0;
if (left < right){
//Divide the sequence into two sub areas to calculate the key value
pivot = partition(arr,left,right);
//Recursive left subarea
quickSort(arr,left,pivot - 1);
//Recursive right subarea
quickSort(arr,pivot + 1,right);
}
}
private static int partition(int[] arr, int left, int right) {
//Cache the first element of the sequence as a keyword
int pivotKey = arr[left];
//Loop condition, two pointers left，right，Points to both ends of the sequence until the two pointers coincide
while(left < right){
//Compare the rightmost value of the sequence with the keyword. If it is greater than, it should be placed to the right of the keyword, right Minus 1 (move one to the left) until the condition is not met
while(left < right && arr[right] >= pivotKey){
right--;
}
//When right When the first one is less than the keyword or two pointers are coincident, assign the value to the position pointed by the left pointer, left Move right. right Stop moving
arr[left++] = arr[right];
//Move now left
while(left < right && arr[left] <= pivotKey){
left++;
}
//When left When the first is greater than the keyword or two pointers are coincident, assign the value to the position pointed to by the right pointer, lright Move left. left Stop moving
arr[right--] = arr[left];
}
arr[left] = pivotKey;//Assign keywords to left
return left;//Return key location
}
}```
```Analysis: algorithm instability, space cost [worst O(n), best peace average O(logn)], time cost [worst O(n*n), best peace average O(nlogn)]
```

Posted by Amman-DJ on Thu, 28 May 2020 09:15:28 -0700