'What is the name of the this sorting method?
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;
}
}
Solution 1:[1]
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
Solution 2:[2]
It should be bubble sort.
But you have wrong implementation.
Take a look on:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | GeertPt |
| Solution 2 | omer blechman |
