首頁 > Java > java教程 > 主體

Java 中的選擇排序

WBOY
發布: 2024-08-30 15:30:50
原創
831 人瀏覽過

Java中的選擇排序是一種不斷尋找未排序部分中最小元素並將其保留在開頭的排序方法(用於升序排序)。過程將重複進行,直到輸入數組完成排序。此外,在選擇排序中,我們將輸入數組分為兩個子數組,其中一個數組用於排序元素,另一個數組用於未排序元素。一開始,已排序的子數組中不會有任何元素。讓我們在下一節中詳細了解選擇排序的工作原理。

選擇排序在 Java 中的工作原理

選擇排序以簡單的方式工作,它從輸入數組中保留兩個子數組。他們是:

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

  • 已排序的子數組以保留已排序的元素
  • 未排序的子數組,用於保留未排序的元素。

演算法

以下是用來選擇排序的演算法

  1. 將最小 (MIN) 指標設為位置 0。
  2. 從陣列的元素列表中找出最小的元素
  • 將最小元素與位置 0 交換
  1. 將 MIN 指標移到下一個位置
  2. 重複此過程,直到輸入陣列已排序。

讓我們透過一個例子來理解選擇排序。以下是必須排序的輸入數組。粗體藍色的元素將作為排序數組的一部分。

Java 中的選擇排序

第 1 步:將 MIN 指標設定到第一個位置。所以,MIN指針指向15。

Java 中的選擇排序

最小:= 15

第 2 步:透過與其餘元素進行比較來找到最小元素。比較15和21,15是最小的。所以,在這種情況下,最小的不會改變。

Java 中的選擇排序

最小:= 15

比較15和6,6是最小的。

Java 中的選擇排序

最小:= 6

比較6和3,3是最小的。

Java 中的選擇排序

最小:= 3

在這種情況下 3 也會更小,因為 19 大於 3。

Java 中的選擇排序

最小:= 3

Java 中的選擇排序

最小:= 3

最後,在本次迭代中,發現 3 是最小的。

第 3 步:將最小元素與位置 0 的元素交換。

Java 中的選擇排序

第 4 步: 將 MIN 指標增加到下一個位置。

Java 中的選擇排序

第 5 步: 透過與其餘元素進行比較來找到下一個最小元素。

Java 中的選擇排序

最小:= 21

Java 中的選擇排序

最小:= 6

Java 中的選擇排序

最小:= 6

Java 中的選擇排序

最小:= 6

Java 中的選擇排序

最小:= 6

第 6 步: 將最小元素與位置 1 的元素交換。

Java 中的選擇排序

重複此過程,直到形成排序數組,如下所示。

Java 中的選擇排序

用 Java 實作選擇排序的範例

如上所述,選擇排序是基於查找最小值和交換。現在,讓我們看看如何使用 Java 實作選擇排序。

使用選擇排序對數組中的元素進行排序的 Java 程式

代碼:

import java.util.*;
public class SelSortExample {
//Method that implements Selectionsort
public static void selsort(int[] arr)
{
int n=arr.length; //length of the array
for(int i=0;i<n-1;i++)
{
int MIN=i; //set the first position as minimum
System.out.println("Sorting based on Number "+(i+1));
//Find the smallest element by comparing with the element in MIN position
for(int j=i+1;j<n;j++)
{
System.out.println("Comparing "+ arr[MIN] + " and " + arr[j]);
if(arr[j]<arr[MIN])
{
System.out.println(arr[MIN] + " is greater than " + arr[j] );
MIN=j;
}
}
//Swap the smallest element with element in MIN position
int temp=arr[i];
arr[i]=arr[MIN];
arr[MIN]=temp;
}
}
public static void main(String[] args) {
int[] arr= {15,21,6,3,19,20}; // input array
System.out.println("Elements in the array before Sorting: "+ Arrays.<em>toString</em>(arr));
<em>selsort</em>(arr);//calling the selection sort method
System.out.println("Elements in the array after Sorting: "+Arrays.<em>toString</em>(arr));
}
}
登入後複製

範例輸出:

Java 中的選擇排序

在上面的程式中,我們有兩個方法-main方法和sell sort方法。 main 方法呼叫 sell sort 方法,傳遞輸入陣列作為參數。最小元素將被識別並與 MIN 指向的元素交換。

The selection sort can also be used where the input array is not defined in code. Let us see how it works using the below program.

Java Program to Sort the Elements using Selection Sort

Code:

import java.util.*;
public class SelectionSortExample
{
public static void main(String args[])
{
int n, i, j, tempvar;
Scanner <u>sc</u> = new Scanner(System.in); //To take the input from user
System.out.print("Enter the size of array : \n");
n = sc.nextInt();
int array[] = new int[n]; //<u>initialising</u> the array
System.out.print("Enter the elements that need to be inserted in the array : \n");
//inserting the elements to the array
for(i=0; i<n; i++)
{
array[i] = sc.nextInt();
}
System.out.print("array before Sorting: \n"+ Arrays.toString(array));
System.out.print("\nSorting begins here..\n");
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(array[i] > array[j])
{
tempvar = array[i];
array[i] = array[j];
array[j] = tempvar;
}
}
}
System.out.print("Array after Sorting is :\n");
for(i=0; i<n; i++)
{
System.out.print(array[i]+ " ");
}
}
}
登入後複製

Sample Output:

Java 中的選擇排序

Here, the input elements given by the user will be compared with the temporary variable and swapped. The process will be repeated until a sorted array is formed.

Performance of Selection Sort

This sorting technique is used for its simplicity and certain other performance advantages over other more sorting techniques.

Java 中的選擇排序

Conclusion

The selection sort does not work efficiently on large lists as it consumes more time for comparison. Selection sort is a method in which an input array will be divided into two subarrays in order to keep them sorted and unsorted elements. The minimum element in the array will be swapped with the element in the first position, and the process continues until a sorted array is formed.

以上是Java 中的選擇排序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!