Sorting Program For this assignment, you are to write a comparative algorithm th
ID: 3743268 • Letter: S
Question
Sorting Program For this assignment, you are to write a comparative algorithm that will compare the efficiency of three sorting routines.
Here is the approach you should use:
1. Write a Bubble Sort function with a test program.
2. Write a Shell Sort function with a test program.
3. Write a Quick Sort function (using stacks, rather than recursion) with a test program.
4. Write the main program (that will include the other 3 programs). Notes: In the test programs, the logic should be as follows: Print the random numbers Sort the numbers keeping track of the costs Print the sorted numbers Print the cost
Use __INCLUDE_LEVEL__ in all of the programs, which includes the sorting routines and a test program. ( Sample program using include level) The main program should have the following logic.
Generate 20 random numbers.
Print the random numbers.
Move the original random numbers into another array.
Bubble Sort the other array.
Print the sorted numbers AND the cost of sorting the numbers.
Move the original random numbers into another array. Shell Sort the other array.
Print the sorted numbers AND the cost of sorting the numbers.
Move the original random numbers into another array. Quick Sort the other array.
Print the sorted numbers AND the cost of sorting the numbers.
To compute the cost, you assign values to every swap and every compare. A compare costs 1 unit, and a swap costs 6 units.
Explanation / Answer
//please save file as Sort.java
import java.io.IOException;
import java.util.Scanner;
public class Sort {
int arr1[],temp,i,j,num,count = 0;
Scanner sc = new Scanner(System.in);
public void getData()throws IOException {
System.out.println("Enter the number of elements to be sorted: ");
num = sc.nextInt();
System.out.println("Enter the numbers: ");
arr1 = new int[50];
for(i=0;i<num;i++)
{
arr1[i] = sc.nextInt();
}
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
System.out.println("The number of swaps are as follows: "+count);
}
public void shell( int arr[], int n)throws IOException
{
for (int gap = n/2; gap > 0; gap /= 2)
{
for (int i = gap; i < n; i += 1)
{
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
arr[j] = temp;
}
}
return 0;
}
quickSort(arr[], low, high)
{
if (low < high)
{
pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
}
-----------------------------------------------
//please save file as Sorting.java
import java.io.IOException;
import java.util.Scanner;
public class Sorting {
public static void main(String[] args) throws IOException {
Sort st=new Sort();
st.getData();
System.out.println("=================Sorting Algorithms=================");
System.out.println("1. Bubble Sort");
System.out.println("2. Shell Sort");
System.out.println("3. quick Sort");
System.out.println("Enter your choice");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
if(choice == 1)
st.bubble();
else if(choice == 2)
st.shell();
else if(choice ==3)
st.quick();
st.print();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.