/**
* encoding: utf-8
* 版权所有 2023 ©涂聚文有限公司
* 许可信息查看: https://www.geeksforgeeks.org/sorting-algorithms/
* 描述: https://www.geeksforgeeks.org/sorting-algorithms/
* # Author : geovindu,Geovin Du 涂聚文. *
* # IDE : IntelliJ IDEA 2023.1 Java 21
* # Datetime : 2023 - 2023/9/28 - 9:55
* # User : geovindu
* # Product : IntelliJ IDEA
* # Project : EssentialAlgorithms
* # File : SortingAlgorithm.java
* # explain : 学习 Sorting Algorithms 类
**/
package SortingAlgorithms;
import java.util.Arrays;
public class SortingAlgorithm {
/**
* 1。Bubble Sort冒泡排序法
* @param array 整数数组
*
* */
public static void BubbleSort(int array[]) {
int size = array.length;
// loop to access each array element
for (int i = 0; i < size - 1; i++)
// loop to compare array elements
for (int j = 0; j < size - i - 1; j++)
// compare two adjacent elements
// change > to < to sort in descending order
if (array[j] > array[j + 1]) {
// swapping occurs if elements
// are not in the intended order
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
/**
* 2 Selection Sort 选择排序
* @param array 整数数组
*/
public static void SelectionSort(int array[]) {
int size = array.length;
for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {
// To sort in descending order, change > to < in this line.
// Select the minimum element in each loop.
if (array[i] < array[min_idx]) {
min_idx = i;
}
}
// put min at the correct position
int temp = array[step];
array[step] = array[min_idx];
array[min_idx] = temp;
}
}
/**
* 3.Insertion Sort 插入排序
* @param array
* */
public static void InsertionSort(int array[]) {
int size = array.length;
for (int step = 1; step < size; step++) {
int key = array[step];
int j = step - 1;
// Compare key with each element on the left of it until an element smaller than
// it is found.
// For descending order, change key<array[j] to key>array[j].
while (j >= 0 && key < array[j]) {
array[j + 1] = array[j];
--j;
}
// Place key at after the element just smaller than it.
array[j + 1] = key;
}
}
}
/**
* encoding: utf-8
* 版权所有 2023 ©涂聚文有限公司
* 许可信息查看:
* 描述:
* # Author : geovindu,Geovin Du 涂聚文.
* # IDE : IntelliJ IDEA 2023.1 Java 17
* # Datetime : 2023 - 2023/9/28 - 10:00
* # User : geovindu
* # Product : IntelliJ IDEA
* # Project : EssentialAlgorithms
* # File : SortingExmaple.java
* # explain : 学习 Sorting Algorithms 类
**/
package BLL;
import SortingAlgorithms.SortingAlgorithm;
import java.util.Arrays;
public class SortingExmaple {
/**
*1.Bubble Sort冒泡排序
* */
public static void Bubble()
{
int[] geovindu = { -2, 45, 0, 11, -9 };
// call method using class name
SortingAlgorithms.SortingAlgorithm.BubbleSort(geovindu);
System.out.println("1.冒泡排序 Sort Bubble Sorted Array in Ascending Order:");
System.out.println(Arrays.toString(geovindu));
}
/**
* 2 Selection Sort 选择排序
*/
public static void Selection()
{
int[] geovindu = { 20, 12, 10, 15, 2 };
//SelectionSort ss = new SelectionSort();
SortingAlgorithms.SortingAlgorithm.SelectionSort(geovindu);
System.out.println("2.选择排序 Selection Sorted Array in Ascending Order: ");
System.out.println(Arrays.toString(geovindu));
}
/**
* 3. Insertion Sort 插入排序
* */
public static void Insertion()
{
int[] geovindu = { 9, 5, 1, 4, 3 };
SortingAlgorithms.SortingAlgorithm.InsertionSort(geovindu);
System.out.println("3.插入排序 Insertion Sorted Array in Ascending Order: ");
System.out.println(Arrays.toString(geovindu));
}
}
调用:
/**
* encoding: utf-8
* 版权所有 2023 ©涂聚文有限公司
* 许可信息查看:
* 描述:
* # Author : geovindu,Geovin Du 涂聚文. *
* # IDE : IntelliJ IDEA 2023.1 Java 21
*
* # Datetime : 2023 - 2023/9/28 - 9:55
* # User : geovindu
* # Product : IntelliJ IDEA
* # Project : EssentialAlgorithms
* # File : Main.java
* # explain : 学习 Sorting Algorithms 类
**/
import BLL.SortingExmaple;
public class Main {
/**
*
* */
public static void main(String[] args)
{
System.out.println("Hello world! Java, 涂聚文 geovindu Geovin Du 学习Java");
// 1.Bubble Sort冒泡排序法
SortingExmaple.Bubble();
//2.
SortingExmaple.Selection();
//3.
SortingExmaple.Insertion();
}
}