Question

Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to...

Design a program that allows you to experiment with different sort algorithms in Java. This program should allow

you to easily plug-in new sort algorithms and compare them. Assume that input data is generated randomly and stored in

a text file (have no less than 2000 items to sort). Do not restrict your program to only one data type, or to one ordering relationship.

The data type, ordering relationship, and the sorting method must be input parameters for your program.

It must produce a comparison chart of all implemented sort algorithms (consider both comparisons and exchanges)

(So must show how many times its compared an exchanged (swapped).

. Start with insertion, selection and bubble sorts (other sorts will be included later.

This is for Data and Algorithm. Plese, help I will upvote. I promise!

                                              Please include this

  1. Submit a design sheet presenting the overall structure of your program
  2. pseudo-code descriptions of implemented sort algorithms,
  3. extended explanation of all of the results that your program generates, the code, and all outputs. HEre is an example of how all of them should look. I have a bubble sort which counts the comparison and exchange.

    public class BubbleSortingMain {

       public static void main(String[] args) {
           int array1[] = {1,2,3,4,5,6,7,8,9,10}; // best case
           int array2[]= {10,9,8,7,6,5,4,3,2,1};//worst case
           bubbleSort(array1);
           bubbleSort(array2);
       }

    public static void bubbleSort(int[] array) {
       int numberOfItems = array.length;
    int comp=0,exch=0,temp=0;
    boolean cont = true;
       for (int pass=1; pass != numberOfItems; pass++)   {
    if (cont) {
    cont = false;
    for (int index=0; index != numberOfItems-pass; index++) {
                                   comp++;
           if (array[index]> array[index+1]) {
               exch++;
                    temp = array[index];
           array[index] = array[index+1];
           array[index+1] = temp;
    cont = true;
           }
       }
    }
    else
    break; // end outer if
    }  
       System.out.println ("comparsion: " + comp); // end inner if
       System.out.println ("exchange: " + exch); // end inner for
    }

    }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

pseudocode in code comments :

public class Sorting {

    public static void main(String[] args) {
        int array1[] = {1,2,3,4,5,6,7,8,9,10}; // best case
        int array2[]= {10,9,8,7,6,5,4,3,2,1};//worst case
        bubbleSort(array1);
        bubbleSort(array2);
        int array3[] = {1,2,3,4,5,6,7,8,9,10}; // best case
        int array4[]= {10,9,8,7,6,5,4,3,2,1};//worst case
        selectionSort(array3);
        selectionSort(array4);
        int array5[] = {1,2,3,4,5,6,7,8,9,10}; // best case
        int array6[]= {10,9,8,7,6,5,4,3,2,1};//worst case
        insertionSort(array5);
        insertionSort(array6);
    }
    public static void printArrray(int[] array){
        int numberOfItems = array.length;
        for(int i=0;i<numberOfItems;i++){
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
    /*
     In insertion sort we make space for the current element in the array , and then insert the current element at that position
     ALGORITHM :
         1.Loop until pass = 1 to number of items
         2.let key be the current element
         3.For each element left to the current element , if it is greater than curr element , we exchange pass and index
         4.Thus at the end of loop we ll be left with a position suitable for key
         5.We insrt key into that position
     */
    public static void insertionSort(int[] array){
        System.out.println("********* INSERTION SORT ************");
        printArrray(array);
        int numberOfItems = array.length;
        int comp=0,exch=0;
        for (int pass = 1; pass < numberOfItems; ++pass) {
            int key = array[pass];
            int index = pass - 1;
            comp+=index;
            while (index >= 0 && array[index] > key) {
                exch++;
                array[index+1] = array[index];
                index = index - 1;
            }
            exch++;
            array[index+1] = key;
        }
        System.out.println ("comparsion: " + comp);
        System.out.println ("exchange: " + exch);
        printArrray(array);
        System.out.println();
    }
    /*
        In selection sort we select an element and find the minimum element on the right of that element
        and then exchange the element with the selected element.
        Hence at any stage of selection sort the left of selected element will always be sorted
        ALGORITHM :
            1.LOOP from (0 to length of array-1)
            2.let curr be the index current element
                store curr in temp variable minI
            3.loop from curr to the end of array to find minium number
                if(array[index]<array[minI]
                    minI = index
            4.After inner loop exchange elements at curr and minI
     */
    public static void selectionSort(int[] array){
        System.out.println("********** SELECTION SORT ***********");
        printArrray(array);
        int numberOfItems = array.length;
        int comp=0,exch=0;
        for (int pass=0; pass<numberOfItems-1; pass++)
        {
            int minI = pass;
            for (int index = pass+1; index < numberOfItems; index++) {
                comp++;
                if (array[index] < array[minI])
                    minI = index;
            }
            exch++;
            int temp = array[minI];
            array[minI] = array[pass];
            array[pass] = temp;
        }
        System.out.println ("comparsion: " + comp);
        System.out.println ("exchange: " + exch);
        printArrray(array);
        System.out.println();
    }
    public static void bubbleSort(int[] array) {
        System.out.println("********* BUBBLE SORT ***********");
        printArrray(array);
        int numberOfItems = array.length;
        int comp=0,exch=0,temp=0;
        boolean cont = true;
        for (int pass=1; pass != numberOfItems; pass++)   {
            if (cont) {
                cont = false;
                for (int index=0; index != numberOfItems-pass; index++) {
                    comp++;
                    if (array[index]> array[index+1]) {
                        exch++;
                        temp = array[index];
                        array[index] = array[index+1];
                        array[index+1] = temp;
                        cont = true;
                    }
                }
            }
            else
                break; // end outer if
        }
        System.out.println ("comparsion: " + comp); // end inner if
        System.out.println ("exchange: " + exch); // end inner for
        printArrray(array);
        System.out.println();
    }
}

Add a comment
Know the answer?
Add Answer to:
Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objective: in Java Write a program that implements 3 sorting algorithms and times them in real ti...

    Objective: in Java Write a program that implements 3 sorting algorithms and times them in real time. These algorithms will sort Cylinders by their volume. First, download the driver and include it in your project. Write a class Cylinder with the following properties baseRadius: a non-negative number that corresponds to the Cylinder’s base’s radius. height: a non-negative number that corresponds to the Cylinder’s height. Next, write a class Sorter with the following bubbleSort: This static method takes in an array...

  • Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the a...

    Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the array. On the first pass you look at all the pairs of items, a[j] and a[j+1], where j is odd (j = 1, 3, 5, …). If their key values are out of order, you swap them. On the second pass you do the same for all the even values (j = 2, 4, 6, …). You do these two passes repeatedly until...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

  • Lab 10A Measure the program execution time One easy way to measure the program execution time is ...

    Lab 10A Measure the program execution time One easy way to measure the program execution time is encapsulate the useful timing functionality of C++ chrono library, This is illustrated inhttps://www.learncpp.com/cpp-tutorial/8-16-timing-your-code/ The example usingChronoTimer.cpp (Github/m10) is an example program using this chrono Timer class object to measure the Sorting on on an integer array of 10000 random integers based on the Bubble Sort vs. the C++ built-in Sort (an https://www.quora.com/Which-sorting-algorithm-does-STL-standard-template-library-use-in-c++. ) Please measure the performance of sorting the same array used...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • A test harness program for testing sorting methods is provided with the rest of the textbook...

    A test harness program for testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch11 package. The program includes a swap method that is used by all the sorting methods to swap array elements. Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. Implement your approach. Test your new program by...

  • you will analyse two algorithms for finding the median of an array of integers. You will...

    you will analyse two algorithms for finding the median of an array of integers. You will compare both algorithms in terms of timing, and hopefully design a hybrid algorithm that uses both, depending on input size. You will write a report describing your experiments and results. the following Java program implements two algorithms for finding the median of an array of integers. The first uses merge sort, and the other implements the recursive linear time selection algorithm, the task is...

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT