-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorts.notes
More file actions
153 lines (112 loc) · 3.74 KB
/
sorts.notes
File metadata and controls
153 lines (112 loc) · 3.74 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
-----------------------------------------------------------
Introduction to Sorting Algorithms
-----------------------------------------------------------
Sorting: Arranging the elements in a collection in an order of some property
Classification:
1. Time Complexity
2. Space Complexity
3. Stability
-> preserve related order of elements
Internal vs External Sort:
ram disk
Recursive or Not-Recursive:
Selection Sort:
Linear scan for the lowest, then add to a new array which is sorted, or swapp with the curr at currenct pass
Time Complexity -> n^2
Space Complexity -> n or if you make swaps, which gives n^2
always n squared since need to find min in each iteration anyway
Bubble Sort:
Bubble up the current largest element
always n^2
Insertion Sort:
essentional, you bubble downwards using a while loop
bestcase -> n
fastest since in best case it's n, however, still n^2 in worst case(reverse sored array)
Merge Sort:
Quick Sort:
[java]{
public class Main {
public static void main(String[] args) {
int[] arr = {1,2,5,3,2,5,6,5,5,3,1,8,9};
selectionSort(arr);
for(int curr : arr){
System.out.print(curr);
}
}
public static void bubbleSort(int[] arr){
for(int i = 0; i<arr.length-1; i++){
for(int j = 0; j<arr.length-1; j++){
if(arr[j]>arr[j+1]){
int placeHolder = arr[j];
arr[j] = arr[j+1];
arr[j+1] = placeHolder;
}
}
}
}
public static void selectionSort(int[] arr){
for(int i = 0; i<arr.length-1; i++){
int min = i;
for(int j = i; j<arr.length; j++){
if(arr[j] < arr[min]){
min = j;
}
}
int holderCurr = arr[i];
arr[i] = arr[min];
arr[min] = holderCurr;
}
}
public static void insertionSort(int[] arr){
for(int i = 1; i<arr.length-1; i++){
int value = arr[i];
int j = i;
while(j > 0 && arr[j-1] > value){
int placeholder = arr[j];
arr[j] = arr[j-1] ;
arr[j-1] = placeholder;
j--;
}
}
}
public static void merge(int[] arr, int l, int r, int m){
int leftPointer = 0;
int rightPointer = 0;
int k = l;
int[] left = new int[m-l+1];
int[] right = new int[r-m];
for(int i = 0; i < left.length; i++){
left[i] = arr[l+i];
}
for(int i = 0; i< right.length; i++){
right[i] = arr[m+1+i];
}
while( k < r+1 ){
if(leftPointer == left.length && rightPointer <= right.length){
arr[k] = right[rightPointer];
rightPointer++;
}
else if(rightPointer == right.length && leftPointer < left.length){
arr[k] = left[leftPointer];
leftPointer++;
}
else if(left[leftPointer] < right[rightPointer]){
arr[k] = left[leftPointer];
leftPointer++;
} else {
arr[k] = right[rightPointer];
rightPointer++;
}
k++;
}
}
public static void mergeSort(int[] arr, int l, int r){
if(l<r){
int mid = (l+r)/2;
mergeSort(arr, l, mid);
mergeSort(arr, mid+1, r);
merge(arr, l, r, mid);
}
}
}
}[java]