简体   繁体   English

这是选择排序还是冒泡排序?

[英]Is this a selection sort or a bubble sort?

Is this a selection sort?这是选择排序吗? I think it is Bubble Sort because I'm using (dot)compareTo.我认为它是冒泡排序,因为我使用的是 (dot)compareTo。 I look at different sources on the internet so I can make one.我在互联网上查看了不同的来源,所以我可以制作一个。 Here is the codes.这是代码。

import java.util.Arrays;

public class SelectionSort {
    public static void main(String args[]) {
        String[] row = {"apple", "orange", "banana", "grapes", "mango", "avocado"};
        int min  = row.length;
        for(int m = 0; m < min-1; m++) {
            for (int n = m+1; n < row.length; n++) {
                if(row[m].compareTo(row[n]) > 0){
                    String bar = row[m];
                    row[m] = row[n];
                    row[n] = bar;
                }
            }
        }
        System.out.println("Expected Outcome: " + Arrays.toString(row));
    }
}

this is not selection sort I selection sort in each iteration you find minimum value and put it to the proper location.这不是选择排序我在每次迭代中选择排序你找到最小值并将其放在适当的位置。 See this picture看到这张图片
在此处输入图片说明

A simple implementation show here: https://www.javatpoint.com/selection-sort-in-java一个简单的实现在这里展示: https : //www.javatpoint.com/selection-sort-in-java

public class SelectionSortExample {  
    public static void selectionSort(int[] arr){  
        for (int i = 0; i < arr.length - 1; i++)  
        {  
            int index = i;  
            for (int j = i + 1; j < arr.length; j++){  
                if (arr[j] < arr[index]){  
                    index = j;//searching for lowest index  
                }  
            }  
            int smallerNumber = arr[index];   
            arr[index] = arr[i];  
            arr[i] = smallerNumber;  
        }  
    }  
       
    public static void main(String a[]){  
        int[] arr1 = {9,14,3,2,43,11,58,22};  
        System.out.println("Before Selection Sort");  
        for(int i:arr1){  
            System.out.print(i+" ");  
        }  
        System.out.println();  
          
        selectionSort(arr1);//sorting array using selection sort  
         
        System.out.println("After Selection Sort");  
        for(int i:arr1){  
            System.out.print(i+" ");  
        }  
    }  
}  

It is not Selection Sort (Milad already answered this), but it is also not Bubble Sort.它不是选择排序(Milad 已经回答了这个),但它也不是冒泡排序。

The way you can tell that it is not bubble sort, is because bubble sort compares pairs of items that are next to each-other (ex: compares items at index 0-1, 1-2, 2-3... and swaps if necessary).您可以判断它不是冒泡排序的方式是因为冒泡排序比较彼此相邻项目对(例如:比较索引 0-1、1-2、2-3... 处的项目并交换如有必要)。 In your code, when m=0, the inner loop will compare item at index 0 with all the other items in the array.在您的代码中,当 m=0 时,内循环会将索引 0 处的项目与数组中的所有其他项目进行比较。

Bubble Sort in Java: Java中的冒泡排序:

public void sort( int[] array) {

    boolean isSorted;
    for (var i = 0; i < array.length; i++) { 
        isSorted = true;
        for (var j = 1;  j < array.length - i ; j++) 
            if (array[j] < array[j - 1]) {           
                swap(array, j, j - 1);
                isSorted = false;
            }
        if (isSorted)  
            return;
    }
}


private void swap(int[] array, int index1, int index2) {
    var temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM