Detailed explanation of Algorithm_analysis case of Java
- 2021-11-13 01:36:03
- OfStack
/*
Bubble sorting: double-layer cycle
1. Outer loop: control the number of sorting rounds, and the length of sorting array decreases 1 (Finally 1 The secondary loop is only left 1 Elements, no comparison is required, and the array has been sorted.
2. Inner loop: Compare the sizes of adjacent elements of the array to determine whether to exchange positions, and the number of comparisons and exchanges decreases with the number of sorting rounds.
*/
public class BubbleSort {
public void sort(int[] array){
for(int i=1;i<array.length;i++){// Number of control wheels
// Compare two adjacent elements, and the larger number bubbles backward
for(int j=0;j<array.length-i;j++){// Control the number of exchanges
if(array[j]>array[j+1]){// No. 1 1 The number is greater than the first 2 Number, exchange
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
System.out.println(" No. 1 -------"+j+"------- Times ");
}
System.out.println(" No. 1 "+i+" Wheel ");
showArray(array);
}
}
showArray(array);
}
/*
Display array
*/
public void showArray(int[] array){
for(int i:array){// Traversing an array
System.out.print(" " "+i);
}
System.out.println();
}
public static void main(String[] args) {
// Create 1 The elements of this array are out of order
int[] array = {63,4,24,1,3,15};
System.out.println(" Array length: "+array.length);
System.out.println("=========================");
// Create a bubble sort class object
BubbleSort sorter = new BubbleSort();
// Call the sort method to sort the array
sorter.sort(array);
}
Display results
Array length: 6
=========================
The--------times
Round 1
"4" 63 "24" 1 "3" 15
Number----1---
Round 1
"4" 24 "63" 1 "3" 15
The------the second time
Round 1
"4" 24 "1" 63 "3" 15
The-----3---times
Round 1
"4" 24 "1" 3 "63" 15
No.----No. 4---
Round 1
"4" 24 "1" 3 "15" 63
Round 2
"4" 24 "1" 3 "15" 63
Number----1---
Round 2
"4" 1 "24" 3 "15" 63
The------the second time
Round 2
"4" 1 "3" 24 "15" 63
The-----3---times
Round 2
"4" 1 "3" 15 "24" 63
The--------times
Round 3
"1" 4 "3" 15 "24" 63
Number----1---
Round 3
"1" 3 "4" 15 "24" 63
Round 3
"1" 3 "4" 15 "24" 63
Round 4
"1" 3 "4" 15 "24" 63
Round 4
"1" 3 "4" 15 "24" 63
Round 5
"1" 3 "4" 15 "24" 63
"1" 3 "4" 15 "24" 63
/* Select Sort Directly: Specifies the sort position to compare with other elements. The number of exchanges is reduced. */
public class SelectSort {
public void sort(int[] array) {
int index;
for (int i = 1; i < array.length; i++) {
index = 0;
for (int j = 1; j <= array.length - i; j++)
if (array[j] > array[index]) {
index = j;
}
// Switch position array.length-i And index( Maximum)
int temp = array[array.length-i];
array[array.length - i] = array[index];
array[index] = temp;
}
showArray(array);
}
/*
Display array
*/
public void showArray(int[] array) {
for (int i : array) {// Traversing an array
System.out.print(" " " + i);
}
System.out.println();
}
public static void main(String[] args) {
// Create 1 The elements of this array are out of order
int[] array = {63, 4, 24, 1, 3, 15};
System.out.println(" Array length: " + array.length);
System.out.println("=========================");
// Create a bubble sort class object
SelectSort sorter = new SelectSort();
// Call the sort method to sort the array
sorter.sort(array);
}
}
Run results:
Array length: 6
=========================
"1" 3 "4" 15 "24" 63