0

I have created 3 sorting methods. My problem is I need to have the user tell the program which sorting method they would like the computer to use. This sounds simple but I am confused on how to do this. My classes are below. If someone could help me out with my problem then thank you.

Selection Sort:

public class SelectionSort {
	public static void main(String[] args) {
		sortTest(500);
	}
	
	private static void sortTest(int n) {
		int[] a = new int[n];
		fillRandom( a, 200, 1200);
		printArray(a, "Values before the sort");
		selection_srt(a);
		printArray(a, "Values after the sort");
	}

	private static void fillRandom(int[] a, int from, int to) {
		int range = (to-from) + 1;
		for (int i = 0; i <a.length; i++) {
			a[i] = from + (int)(Math.random() * range);
		}
	}
	
	private static void printArray(int[] a, String label) {
		System.out.print(label + ": ");
		for (int i = 0; i <a.length; i++) {
			System.out.print(a[i]+" ");
		}
		System.out.println();
	}
	

	public static void selection_srt(int array[]) {
		for (int x=0; x<array.length-1; x++) { 
			int index_of_min = x;
			for (int y=x+1; y<array.length; y++) { 
				if (array[index_of_min]<array[y]) {
					index_of_min = y;
				}
			}
			if (x!=index_of_min) { 
				int temp = array[x];
				array[x] = array[index_of_min];
				array[index_of_min] = temp;
			}
		}
	}
}

Insertion Sort

public class InsertionSort{
public static void main(String arg[]){
int n = 500;//sets number of integers array uses
int[] a = new int[n]; //sets array to integer n and declares array
for(int i=0;i<n;i++){//sets range for i
}
for (int i = 1; i < a.length; i++) {//array loop
int j = i; //sets value of j to i
int B = a[i] = 200 + (int)(Math.random() * ((1200 - 200) + 1));//generates random numbers from 200 to 1200 for array


while ((j > 0) && (a[j-1] > B)) {//if this is true than the while code will execute
a[j] = a[j-1];//this will execute if the above code runs
j--; //takes away value of j by 1
}
a[j] = B; //sets value of a[j] to B
}
System.out.println(" Insertion Sort :"); //output
for(int i=0;i<a.length;i++)//array loop
{
System.out.print(" " + a[i]);//prints out the array
}
}
}

Bubble Sort:

public class BubbleSort {



public static void main(String[] args)
{

int n = 501; //sets number of integers array uses
int[] a = new int[n];//sets array to integer n and declares array
for(int i=0;i<n;i++)//sets range for i
{
int k = 200 + (int)(Math.random() * ((1200 - 200) + 1));//generates random numbers from 200 to 1200 for array
a[i] = k;//sets a[i] to value of k
} 
bubble(a);
}


public static void bubble(int[] a)
{

display(a);
System.out.println("\nThe Bubble Sort passes : ");//output that tells you how many times it passed
for(int i=0;i<a.length-1;i++)//array loop
{
System.out.print("Pass "+(i+1)+":\t");//prints out
for(int j=0;j<a.length-1-i;j++)//array loop
{
if(a[j] > a[j+1])//if a[j] is larger than a[j+1] than below code runs
{
int temp = a[j];//sets value of temp to a[j]
a[j] = a[j+1];//sets value of a[j] to a[j+1]
a[j+1] = temp;///sets value of temp to a[j+t]
}
}
display(a);
}
}
static void display(int[] arr)
{
for(int i=0;i<arr.length;i++)//loop for array
System.out.print(arr[i]+"\t"); //prints out array
System.out.println();//output
}




}

And of course this is what I got up to till I got confused

import java.util.Scanner;
public class Client { 
	public static void main(String[] args) {
	
	int choose;
	
	Scanner reader = new Scanner(System.in);
	
	System.out.println("Choose 1 for insertion Sort, 2 for Bubble Sort, or 3 for Selection Sort");
	choose = reader.nextInt();
	
	if
	(choose == 1)
		
	
	
}}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by peter_budo
0

You need to "re-do" your sort classes. Remove main class from all of them, provide a public method which is able to receive a collection of numbers (array, list, vector) and return sorted collection. Then in your client application after taking user choice call for example

int[] unsorted = {3, 5, 1, 2, 6};
int[] sorted = BuubleSort.sort(unsorted);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.