Home > Back-end >  What is the name of the this sorting method?
What is the name of the this sorting method?

Time:02-08

I am learning sorting. but I don't know what this sorting method is called.

class Solution {
    public void sort(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n; i  ) {
            for (int j = 0; j < n; j  ) {
                if (nums[i] > nums[j]) {
                    swap(nums, i, j);
                }
            }
        }
    }

    private void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}

CodePudding user response:

It should be bubble sort.

But you have wrong implementation.

Take a look on:

https://www.geeksforgeeks.org/bubble-sort/

CodePudding user response:

It looks like a selection sort, but it's not correctly implemented.

https://en.wikipedia.org/wiki/Selection_sort

To start, your second for loop should start with j = i 1

  •  Tags:  
  • Related