使用选择排序对数组进行排序
Sorting an array using Selection Sort
我已经实现了使用选择排序对该数组进行排序的代码。代码看起来没问题,但它并没有完美地对数组进行排序。这是要排序的数组,{20, 46, 22, 19, 6, 42, 14, 5, 48, 47, 17, 39, 51, 7, 2}
这是我实现的代码;
public class Main {
public static void main(String[] args) {
int[] arr = {20, 46, 22, 19, 6, 42, 14, 5, 48, 47, 17, 39, 51, 7, 2};
System.out.println("Unsorted Array: \t");
StringBuffer sb = new StringBuffer();
for (Object s : arr) {
sb.append(s);
sb.append(" ");
}
String str = sb.toString();
System.out.println(str);
selectionSort(arr);
System.out.println("Sorted Array: \t");
printArray(arr);
}
public static void selectionSort(int[] arr) {
int n = arr.length;
// one by one move boundary of unsorted subarray
for (int i = 0; i < n - 1; i++) {
// find the minimum element in unsorted array
int min = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min]) {
min = j;
}
//swapping the found min value with the first element
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
}
public static void printArray(int[] arr) {
StringBuffer sb = new StringBuffer();
for (Object s : arr) {
sb.append(s);
sb.append(" ");
}
String str = sb.toString();
System.out.println(str);
}
}
这是我收到的输出;
我不知道为什么它没有正确整理,希望得到一些帮助。
交换发生在内循环之后。喜欢,
public static void selectionSort(int[] arr) {
int n = arr.length;
// one by one move boundary of unsorted subarray
for (int i = 0; i < n - 1; i++) {
// find the minimum element in unsorted array
int min = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
// swapping the found min value with the first element
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
只是改变它(并添加一个 printArray
)我得到
Unsorted Array:
[20, 46, 22, 19, 6, 42, 14, 5, 48, 47, 17, 39, 51, 7, 2]
Sorted Array:
[2, 5, 6, 7, 14, 17, 19, 20, 22, 39, 42, 46, 47, 48, 51]
我已经实现了使用选择排序对该数组进行排序的代码。代码看起来没问题,但它并没有完美地对数组进行排序。这是要排序的数组,{20, 46, 22, 19, 6, 42, 14, 5, 48, 47, 17, 39, 51, 7, 2}
这是我实现的代码;
public class Main {
public static void main(String[] args) {
int[] arr = {20, 46, 22, 19, 6, 42, 14, 5, 48, 47, 17, 39, 51, 7, 2};
System.out.println("Unsorted Array: \t");
StringBuffer sb = new StringBuffer();
for (Object s : arr) {
sb.append(s);
sb.append(" ");
}
String str = sb.toString();
System.out.println(str);
selectionSort(arr);
System.out.println("Sorted Array: \t");
printArray(arr);
}
public static void selectionSort(int[] arr) {
int n = arr.length;
// one by one move boundary of unsorted subarray
for (int i = 0; i < n - 1; i++) {
// find the minimum element in unsorted array
int min = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min]) {
min = j;
}
//swapping the found min value with the first element
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
}
public static void printArray(int[] arr) {
StringBuffer sb = new StringBuffer();
for (Object s : arr) {
sb.append(s);
sb.append(" ");
}
String str = sb.toString();
System.out.println(str);
}
}
这是我收到的输出;
我不知道为什么它没有正确整理,希望得到一些帮助。
交换发生在内循环之后。喜欢,
public static void selectionSort(int[] arr) {
int n = arr.length;
// one by one move boundary of unsorted subarray
for (int i = 0; i < n - 1; i++) {
// find the minimum element in unsorted array
int min = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
// swapping the found min value with the first element
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
只是改变它(并添加一个 printArray
)我得到
Unsorted Array:
[20, 46, 22, 19, 6, 42, 14, 5, 48, 47, 17, 39, 51, 7, 2]
Sorted Array:
[2, 5, 6, 7, 14, 17, 19, 20, 22, 39, 42, 46, 47, 48, 51]