-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortStep.java
More file actions
135 lines (117 loc) · 3.52 KB
/
SortStep.java
File metadata and controls
135 lines (117 loc) · 3.52 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
import java.util.*;
/**
* Driver program for the Sorts class.
*/
public class SortStep{
private Scanner console;
private int [] myArray;
private Sorts mySorts;
private String listType;
/**
* Constructor for the SortStep object
*/
public SortStep(){
console = new Scanner(System.in);
mySorts = new Sorts();
myArray = null;
listType = "Integer";
}
/**
* Asks the user to select a sorting algorithm, fills the array
* with an amount of random integer data chosen by the user, calls
* the sorting algorithm, and gives an option of printing out the
* data after it has been sorted.
*/
public void sortMenu(){
String choice;
do{
System.out.println();
System.out.println("Sorting algorithm menu");
System.out.println();
System.out.println("(1) Bubble sort");
System.out.println("(2) Selection sort");
System.out.println("(3) Insertion sort");
System.out.println("(4) Recursive mergesort");
System.out.println("(5) Fill with Integers");
System.out.println("(Q) Quit");
System.out.println();
System.out.print("Choice ---> ");
choice = console.next() + " ";
if ('1' <= choice.charAt(0) && choice.charAt(0) < '6'){
System.out.println();
mySorts.setStepCount(0);
switch (choice.charAt(0)){
case '1':
resetArray();
mySorts.bubbleSort(myArray);
break;
case '2':
resetArray();
mySorts.selectionSort(myArray);
break;
case '3':
resetArray();
mySorts.insertionSort(myArray);
break;
case '4':
resetArray();
mySorts.mergeSort(myArray, myArray.length);
break;
case '5':
listType = "Integer";
break;
}
if ('1' <= choice.charAt(0) && choice.charAt(0) <= '4'){
System.out.println();
System.out.println("Array sorted to:");
screenOutput();
System.out.println();
System.out.println("# steps = " + mySorts.getStepCount());
System.out.println();
}
}
} while (choice.charAt(0) != 'Q' && choice.charAt(0) != 'q');
}
/**
* Initializes myArray with random integers in the range
* 1..largestInt
*
* @param numInts number of integers to generate (size of
* myArray)
* @param largestInt largest possible random integer to create
*/
private void fillArrayWithInts(){
System.out.print("How many numbers do you wish to generate? ");
int numInts = console.nextInt();
System.out.print("Largest integer to generate? ");
int largestInt = console.nextInt();
myArray = new int[numInts];
//TODO: create code that will put random numbers between 1 and largestInt, inclusive, into array
for(int i = 0; i < myArray.length; i++)
{
}
}
/**
* reset the array for the next sort
*/
private void resetArray(){
if (myArray == null || listType.equals("Integer")){
fillArrayWithInts();
}
System.out.println();
System.out.println("Array reset to:");
screenOutput();
}
/**
* prints out the contents of the array in tabular form, 12 columns
*/
private void screenOutput(){
for (int loop = 0; loop < myArray.length; loop++){
if (loop % 12 == 0){
System.out.println();
}
System.out.print(myArray[loop] + " ");
}
System.out.println();
}
}