简体   繁体   中英

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. 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

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.

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). In your code, when m=0, the inner loop will compare item at index 0 with all the other items in the array.

Bubble Sort in 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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