-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting.java
More file actions
167 lines (127 loc) · 3.73 KB
/
Sorting.java
File metadata and controls
167 lines (127 loc) · 3.73 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
import java.io.*;
// Sorting - Implement two types of sorting algorithms: Merge sort and bubble sort.
class Sorting {
// An implementation of mergesort.
static int[] mergesort(int[] a) {
// Need to use an auxiliary function to call this.
int len = a.length;
// if the array is size 1, then return the array.
if(len == 1) {
return a;
}
int[] sorted = new int[len];
// else split up the array into two, and sort each half.
int half = len / 2;
int[] b = new int[half];
for(int i = 0; i < half; i++) {
b[i] = a[i];
}
int[] c = new int[len-half];
for(int i = 0; i < len-half; i++) {
c[i] = a[i+half];
}
b = mergesort(b);
c = mergesort(c);
sorted = merge(b, c);
return sorted;
}
// A function used to merge two arrays together.
static int[] merge(int[] a, int[] b) {
int[] sorted = new int[a.length + b.length];
int alen = 0;
int blen = 0;
int curr = 0;
while(alen < a.length && blen < b.length) {
// Add the lower value to the sorted array, and increment the pointer to that array.
if(a[alen] <= b[blen]) {
sorted[curr] = a[alen];
alen++;
curr++;
} else {
sorted[curr] = b[blen];
blen++;
curr++;
}
}
// Add the end of the still not empty array.
if(alen == a.length) {
while(blen < b.length) sorted[curr++] = b[blen++];
} else {
while(alen < a.length) sorted[curr++] = a[alen++];
}
return sorted;
}
// A function implementing selectionsort.
static int[] selectionsort(int[] a) {
// Set up a temporary int which will hold array values.
int temp;
// Set up an int which will hold the min
int min;
// Set up a pointer that holds the position up to which the array is sorted.
int idx = 0;
while (idx < a.length) {
min = a[idx];
for(int i = idx; i < a.length; i++) {
if(a[i] < min) {
temp = a[idx];
a[idx] = a[i];
a[i] = temp;
}
}
idx++;
}
return a;
}
// A function implementing bubble sort.
static int[] bubblesort(int[] a) {
// Set a boolean to indicate whether a whole pass has gone through without any swaps
boolean whole = false;
boolean swap = false;
// Set a temporary int which will hold array values for swap.
int temp;
while(!whole) {
for(int i = 0; i < a.length-1; i++) {
if(a[i] > a[i+1]) {
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
swap = true;
}
}
if(!swap) whole = true;
// Reset swap
swap = false;
}
return a;
}
// Test the applications of merge sort and bubble sort.
public static void main(String args[]) {
// Test the mergesort function.
int[] sortthis = {9, 8, 7, 6, 5, 4, 3, 2, 1};
int[] sortthis2 = {9, 8, 7, 6, 5, 4, 3, 2, 1};
int[] sortthis3 = {9, 8, 7, 6, 5, 4, 3, 2, 1};
System.out.print("Trying to sort this array: ");
for(int i = 0; i < sortthis.length; i++) {
System.out.print(sortthis[i] + " ");
}
System.out.println();
int[] sorted = mergesort(sortthis);
System.out.print("Test of the mergesort method: ");
for(int i = 0; i < sorted.length; i++) {
System.out.print(sorted[i] + " ");
}
System.out.println();
sorted = selectionsort(sortthis2);
System.out.print("Test of selection sort: ");
for(int i = 0; i < sorted.length; i++) {
System.out.print(sorted[i] + " ");
}
System.out.println();
sorted = bubblesort(sortthis3);
System.out.print("Test of bubble sort: ");
for(int i = 0; i < sorted.length; i++) {
System.out.print(sorted[i] + " ");
}
System.out.println();
}
}