Selection and sorting algorithm implementation and analysis

Select sort

Basic introduction to selection sorting

  • Selective sorting also belongs to the internal sorting method, which is to select an element from the data to be sorted according to the specified rules, and then exchange positions according to the regulations to achieve the purpose of sorting.

Choice sorting idea

Select sorting is also a simple sorting method. Its basic idea is (n is the size of the array):

  • Select the minimum value from arr[0]~arr[n-1] for the first time and exchange with arr[0]
  • Select the smallest value from arr[1]~arr[n-1] for the second time and exchange with arr[1]
  • For the third time, select the smallest value from arr[2]~arr[n-1] and exchange with arr[2], …,
  • Select the smallest value from arr[i-1]~arr[n-1] for the ith time, and exchange it with arr[i-1], …,
  • Select the smallest value from arr[n-2]~arr[n-1] for the n-1th time and exchange with arr[n-2],
  • A total of n-1 passes are passed to obtain an ordered sequence arranged from small to large according to the sorting code.

Selection sort diagram

Insert picture description here
Insert picture description here

Code

//选择排序
public class SelectSort {
    
    

	public static void main(String[] args) {
    
    
		int[] arr = {
    
     101, 34, 119, 1 };
		selectSort(arr);
	}

	// 选择排序
	public static void selectSort(int[] arr) {
    
    

		// 在推导的过程,我们发现了规律,因此,可以使用for来解决
		// 选择排序时间复杂度是 O(n^2)
		for (int i = 0; i < arr.length - 1; i++) {
    
    
			int minIndex = i;
			int min = arr[i];
			for (int j = i + 1; j < arr.length; j++) {
    
    
				if (min > arr[j]) {
    
     // 说明假定的最小值,并不是最小
					min = arr[j]; // 重置min
					minIndex = j; // 重置minIndex
				}
			}

			// 将最小值,放在arr[0], 即交换
			if (minIndex != i) {
    
    
				arr[minIndex] = arr[i];
				arr[i] = min;
			}

			System.out.println("第" + (i + 1) + "轮后~~");
			System.out.println(Arrays.toString(arr));
		}

	}
}

  • Program running results
1轮后~~
[1, 34, 119, 101]2轮后~~
[1, 34, 119, 101]3轮后~~
[1, 34, 101, 119]

Test selection sort performance

//选择排序
public class SelectSort {
    
    

	public static void main(String[] args) {
    
    

		//创建要给80000个的随机的数组
		int[] arr = new int[80000];
		for (int i = 0; i < 80000; i++) {
    
    
			arr[i] = (int) (Math.random() * 8000000); // 生成一个[0, 8000000) 数
		}
		
		Date data1 = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date1Str = simpleDateFormat.format(data1);
		System.out.println("排序前的时间是=" + date1Str);

		selectSort(arr);

		Date data2 = new Date();
		String date2Str = simpleDateFormat.format(data2);
		System.out.println("排序前的时间是=" + date2Str);

	}

	// 选择排序
	public static void selectSort(int[] arr) {
    
    

		// 在推导的过程,我们发现了规律,因此,可以使用for来解决
		// 选择排序时间复杂度是 O(n^2)
		for (int i = 0; i < arr.length - 1; i++) {
    
    
			int minIndex = i;
			int min = arr[i];
			for (int j = i + 1; j < arr.length; j++) {
    
    
				if (min > arr[j]) {
    
     // 说明假定的最小值,并不是最小
					min = arr[j]; // 重置min
					minIndex = j; // 重置minIndex
				}
			}

			// 将最小值,放在arr[0], 即交换
			if (minIndex != i) {
    
    
				arr[minIndex] = arr[i];
				arr[i] = min;
			}
		}

	}
}

  • Program running results
排序前的时间是=2020-12-1 19:59:19
排序前的时间是=2020-12-1 19:59:20

to sum up

Since the selection sorting algorithm is in the innermost for loop, after the if (min> arr[j])
condition is satisfied, only the minimum value and the index of the minimum value in the array need to be recorded, and there is no need to do it every time like bubble sort Perform exchange operations, so the execution speed of the selection sorting algorithm is faster than the bubble sorting algorithm

Guess you like

Origin blog.csdn.net/m0_46405589/article/details/110442041