Java 中的选择排序,如何改进代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18975050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selection Sort in Java, ways I can improve the code?
提问by Nikhil Gopal
This is the code I have for my selection sort program, I want to know if there's any way of improving the code without using additional methods or classes.
这是我的选择排序程序的代码,我想知道是否有任何方法可以在不使用其他方法或类的情况下改进代码。
public class Selection_Sort {
public static void main(String[] args) {
int arr[]={234,151,123,4,5342,76,48};
int min=0; int temp;
for(int i=0;i<=arr.length-1;i++){
min=i;
for (int k=i+1;k<arr.length;k++){
if(arr[k]<arr[i]){
temp=arr[i];
arr[i]=arr[k];
arr[k]=temp;
}
}
}
for (int j=0;j<=arr.length-1;j++)
System.out.println(arr[j]+" ");
}
}
回答by Praind
Looks like you are using the bubblesort algorithm which is very slow. If you want to improve your code, i would recommend to use an algorithm like ripplesort or quicksort.
看起来您正在使用非常慢的冒泡排序算法。如果您想改进您的代码,我建议您使用像ripplesort 或quicksort 这样的算法。
回答by Vimal Bera
Slight improvement should be like this :
轻微的改进应该是这样的:
int arrayLength = arr.length;
// Then use it in conditional statement of for loop.
So that it won't invoke length property of Array
every time in loop. For small number of loops it doesn't impact much but it will help to reduce the time when loops are more or number of iteration of loop are more.
这样它就不会Array
在循环中每次都调用 length 属性。对于少量循环,它不会产生太大影响,但是当循环更多或循环迭代次数更多时,它将有助于减少时间。
回答by SagersWang
- The value of the local variable min is not used
k <= arr.length-1
- 未使用局部变量 min 的值
k <= arr.length-1
-->
-->
k < arr.length
回答by Hlib Babii
public static void main(String[] args) {
int arr[]={234,151,123,4,5342,76,48};
int arrLength = arr.length;
for(int i=0;i<arrLength-1;i++){
int min=i;
for (int k=i+1;k<arrLength;k++){
if(arr[k]<arr[min]){
min = k;
}
}
if (i != min) {
int temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
}
for (int j=0;j<arrLength;j++) {
System.out.println(arr[j]+" ");
}
}
回答by allocated
Use this
用这个
class Selection {
public static void main(String[] args) {
int arr[]={234,151,123,4,5342,76,48}; /* arr[0] to arr[n-1] is the array to sort */
int lowest, i, j;
for(i = 0 ; i < arr.length-1; i++) { /* advance the position through the entire array */
lowest = i; /* assume the min is the first element */
for(j = i+1 ; j < arr.length; j++) { /* if this element is less, then it is the new minimum */
if(arr[j] < arr[lowest]) {
lowest = j; /* found new minimum; remember its index */
}
}
if(lowest != i) { /* lowest is the index of the minimum element. Swap it with the current position */
int temp = arr[i];
arr[i] = arr[lowest];
arr[lowest] = temp;
}
}
for (int k = 0; k <= arr.length-1 ; k++) {
System.out.println(arr[k] + " ");
}
}
}
This is the selection sort algorithm you asked.
这是您询问的选择排序算法。
回答by realPK
Here is original Selection sort implementation. The implementation in question in not using min to perform the swap operation.
这是原始的选择排序实现。有问题的实现不使用 min 来执行交换操作。
public static void sort(int[] arr) {
int min=-1;
for (int i = 0; i < arr.length; i++) {
min = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[min] > arr[j]) {
min = j;
}
}
if (min != i) {
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
}
回答by user3580320
public class JavaApplication55 {
public static void main(String[] args) {
int[] array ={234,435,567,768,123,456,789,789,5670,6789};
for(int j =0;j< array.length;j++){
for(int i =j+1;i < array.length;i++ ){
int temp;
if(array[j]>array[i]){
temp =array[j];
array[j] =array[i];
array[i] =temp;
}
else{}
}}
for(int k =0;k< array.length;k++){
System.out.println(array[k]);
}
}
enter code here
}
回答by Shiva
Without a method to write a java program on selection sort could be cumbersome. Well, here's the improvised code.
如果没有在选择排序上编写 Java 程序的方法可能会很麻烦。好吧,这是临时代码。
public class JavaSelectionSort
{
public static int[] selectionSort(int[] arr)
{
// selection sort array java
for(int a = 0; a < arr.length - 1; a++)
{
int index = a;
for(int b = a + 1; b < arr.length; b++)
if(arr[b] < arr[index])
index = b;
int smallNumber = arr[index];
arr[index] = arr[a];
arr[a] = smallNumber;
}
return arr;
}
public static void main(String[] args)
{
int[] arrOne = {10, 23, 32, 14, 27, 45, 36, 21};
int[] arrTwo = selectionSort(arrOne);
for(int x: arrTwo)
{
System.out.print(x);
System.out.print(", ");
}
}
}
For more on selection sort refer thisresource.
有关选择排序的更多信息,请参阅此资源。