-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorter.java
More file actions
109 lines (91 loc) · 2.7 KB
/
sorter.java
File metadata and controls
109 lines (91 loc) · 2.7 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
import java.util.Arrays;
import java.io.*;
public class sort {
public static void main(String[] args) throws IOException {
System.out.print("\n**************************\n");
System.out.print("Welcome to Sorter\n");
System.out.print("Ver 0.1\n");
System.out.print("Created 6/5/2015 by Paul Lacey\n\n");
System.out.print("See which sorting algorithm performs the best!\n");
System.out.print("**************************\n\n");
System.out.print("enter csv nums to be sorted:");
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String in = stdin.readLine();
String[] strings = in.split("\\s*,\\s*");
int[] nums = new int[strings.length];
for (int i = 0; i < strings.length; ++i) {
nums[i] = Integer.parseInt(strings[i]);
}
insertionSort(nums);
selectionSort(nums);
bubbleSort(nums);
}
private static void insertionSort(int[] nums) {
int loop = 0;
int[] array = Arrays.copyOfRange(nums, 0, nums.length);
System.out.print("\n********* Insertion Sort **********\n");
// printNumsArray(array);
for (int i = 1; i < array.length; ++i) {
for(int k = i; k > 0 && array[k] < array[k-1]; --k) {
swap(array,k,k-1);
// printNumsArray(array);
++loop;
}
++loop;
}
System.out.println("Iterations: " + loop);
}
private static void selectionSort(int[] nums) {
int loop = 0;
int[] array = Arrays.copyOfRange(nums, 0, nums.length);
System.out.print("\n********* Selection Sort **********\n");
// printNumsArray(array);
for (int i = 0; i < array.length; ++i) {
int k = i;
for (int j = 1 + i; j < array.length; ++j) {
if (array[j] < array[k])
k = j;
printNumsArray(array);
++loop;
}
swap(array, i, k);
// printNumsArray(array);
++loop;
}
System.out.println("Iterations: " + loop);
}
private static void bubbleSort(int[] nums) {
int loop = 0;
int[] array = Arrays.copyOfRange(nums, 0, nums.length);
System.out.print("\n********* Bubble Sort **********\n");
// printNumsArray(array);
for (int i=0; i < array.length; ++i) {
boolean swapped = false;
for(int j = array.length-1; j > i ; --j) {
if(array[j] < array[j-1]) {
swap(array, j, j-1);
swapped = true;
}
if (swapped) {
// printNumsArray(array);
}
++loop;
}
++loop;
if(!swapped)
break;
}
System.out.println("Iterations: " + loop);
}
private static void printNumsArray(int[] array) {
System.out.println(Arrays.toString(array));
}
private static void printStringArray(String[] array) {
System.out.println(Arrays.toString(array));
}
private static void swap(int[] array, int index1, int index2) {
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
}