-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic Sort Algorithm
More file actions
175 lines (159 loc) · 4.96 KB
/
Basic Sort Algorithm
File metadata and controls
175 lines (159 loc) · 4.96 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package pkg.infostuido.algorithmdb.basicalgm;
/**
* Following concepts of sort algorithm come from wikipedia
* source: https://en.wikipedia.org/wiki/Sorting_algorithm
*/
public class SortingAlgm {
/**
* Quick Sort
* Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays.
* O(nlogn)
*/
public void quicksort (double[] queue, int begin, int end){
if(begin<end){
double key = queue[begin];
int i = begin;
int j = end;
while(i<j){
while(i<j && queue[j]>key){
j--;
}
if(i<j){
queue[i] = queue[j];
i++;
}
while(i<j && queue[i] < key){
i++;
}
if(i<j){
queue[j] = queue[i];
j--;
}
}
queue[i] = key;
quicksort(queue,begin, i-1);
quicksort(queue, i+1, end);
}
}
/**
* Merge Sort
* Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
* Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.
* O(nlogn)
*/
public void mergesort(double queue[],double temp[],int m,int n){
if (m == n) return;
int mid = (m+n)/2;
mergesort(queue, temp, m, mid);
mergesort(queue, temp, mid+1, n);
for (int i = m; i <= n; i++) {
temp[i] = queue[i];
}
int index1 = m;
int index2 = mid + 1;
int index = m;
while (index1 <= mid && index2 <= n) {
if (temp[index1] < temp[index2]) {
queue[index++] = temp[index1++];
} else {
queue[index++] = temp[index2++];
}
}
while(index1 <= mid) {
queue[index++] = temp[index1++];
}
while(index2 <= n) {
queue[index++] = temp[index2++];
}
}
/**
* Selection Sort
* O(n2)
*/
public void selectionsort(double[] queue){
for(int i = 0; i < queue.length; i++){
for(int j = i+1; j < queue.length; j++){
if(queue[i] > queue[j]){
double temp = queue[i];
queue[i] = queue[j];
queue[j] = temp;
}
}
}
}
/**
* Bubble Sort
* repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.
* O(n2)
*/
public void bubblesort(double[] queue){
for (int i = 0; i < queue.length; i++) {
for (int j = queue.length - 1; j > i; j--) {
if (queue[j - 1] > queue[j]) {
queue[j - 1] = queue[j - 1] + queue[j];
queue[j] = queue[j - 1] - queue[j];
queue[j - 1] -= queue[j];
}
}
}
}
/**
* Heap Sort
* 1st: In the first step, a heap is built out of the data. The heap is often placed in an array with the layout of a complete binary tree.
* a sorted array is created by repeatedly removing the largest element from the heap (the root of the heap), and inserting it into the array. The heap is updated after each removal to maintain the heap. Once all objects have been removed from the heap, the result is a sorted array.
* worst-case O(nlogn)
*/
public void heapsort(double[] queue){
for(int i = 1; i < queue.length; i++){
}
}
/**
* Insertion Sort
* Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
* О(n2)
*/
public void insertionsort(double[] queue){
for (int j = 1; j < queue.length; j++) {
int i = j - 1;
double key = queue[j];
while (i >= 0 && queue[i] > key) {
queue[i + 1] = queue[i];
queue[i] = key;
i -= 1;
}
}
}
/**
* Shell Sort
*
* Average case: depends on gap
*/
public void shellsort(double[] queue){
double key;
for(int k=queue.length/2; k>0; k/=2){
for(int i = k; i< queue.length; i++){
for(int j = i; j>=k ; j-=k){
if(queue[j-k] > queue[j]){
key = queue[j-k];
queue[j-k] = queue[j];
queue[j]=key;
}
}
}
}
}
public static void main(String[] args) {
double[] queue = {3,60,50,1,-4,99,30};
//double[] temp = new double[queue.length];
SortingAlgm sort = new SortingAlgm();
//sort.bubblesort(queue);
//sort.insertionsort(queue);
//sort.quicksort(queue, 0, 6);
//sort.shellsort(queue);
//sort.mergesort(queue, temp, 0, queue.length-1);
sort.selectionsort(queue);
for(int i=0; i<queue.length; i++){
System.out.println(queue[i]);
}
}
}