-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort.java
101 lines (86 loc) · 2.99 KB
/
Sort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.util.Arrays;
import java.util.Collections;
public class Sort {
/*
Topics Covered:
* Bubble Sort O(n²)
* Selection Sort O(n²)
* Insertion Sort O(n²)
* Inbuilt Sort O(nlogn)
* Counting Sort O(n+range)
*/
public static void main(String[] args) {
System.out.println("Bubble Sort: "+ Arrays.toString(bubbleSort(new int[]{5,4,1,3,2})));
System.out.println("Selection Sort: "+ Arrays.toString(selectionSort(new int[]{5,4,1,3,2})));
System.out.println("Insertion Sort: "+ Arrays.toString(insertionSort(new int[]{5,4,1,3,2})));
int nums[] = new int[]{5,4,1,3,2};
Arrays.sort(nums);
System.out.println("Inbuild Sort: " + Arrays.toString(nums));
Integer nums2[] = {5,4,1,3,2};
Arrays.sort(nums2, Collections.reverseOrder());
System.out.println("Inbuild Sort (reverse) {N.A. for primitive datatype}: " + Arrays.toString(nums2));
System.out.println("Counting Sort: "+ Arrays.toString(countingSort(new int[]{1,4,1,3,2,4,3,7})));
}
public static int[] bubbleSort(int nums[]){
for(int turn=0; turn<nums.length; turn++){
for(int j=0; j<nums.length-turn-1; j++){
if(nums[j]>nums[j+1]){
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
return nums;
}
public static int[] selectionSort(int nums[]){
for(int i=0; i<nums.length; i++){
int smallest_index = i;
for(int j=i+1; j<nums.length; j++){
if(nums[smallest_index]>nums[j]){
smallest_index = j;
}
}
//swap with right position
int temp = nums[i];
nums[i] = nums[smallest_index];
nums[smallest_index] = temp;
}
return nums;
}
public static int[] insertionSort(int nums[]){
for(int i=1; i<nums.length; i++){
int current = nums[i];
int prev = i-1;
//Find right position
while(prev>=0 && nums[prev]>current){
nums[prev+1] = nums[prev]; //push numbers
prev--;
}
//prev == -1, then loop breaks
nums[prev+1] = current;
}
return nums;
}
public static int[] countingSort(int nums[]){
// Find Maximum value
int largest = Integer.MIN_VALUE;
for(int i=0; i<nums.length; i++){
largest = Math.max(largest, nums[i]);
}
// Fill count with frequency of each value
int count[] = new int[largest+1];
for(int i=0; i<nums.length; i++){
int index = nums[i];
count[index]++;
}
// Update original array
int counter = 0;
for(int i=0; i<largest+1; i++){
for(int j=0; j<count[i]; j++){
nums[counter++] = i;
}
}
return nums;
}
}