Question

Hello, I need the correct answer for this please , a quick sort program -java code-...

Hello, I need the correct answer for this please , a quick sort program -java code- that have 2 arrays first one will have different values , the second one the values will be from smallest to largest number then the program have a quick sort to do it for both of them and will count the time - how long the quick sort is take for each one - which array is faster.

Thank you ❤️
0 0
Add a comment Improve this question Transcribed image text
Answer #1
QuickSort.java
public class QuickSort {


        private int array[];
        private int length;

        public void sort(int[] arr) {

            if (arr == null || arr.length == 0) {
                return;
            }
            this.array = arr;
            length = arr.length;
            quickSort(0, length - 1);
        }

        private void quickSort(int lowerIndex, int higherIndex) {

            int i = lowerIndex;
            int j = higherIndex;
            // calculate pivot , COnsider pivot as middle index number
            int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
            // Divide into two arrays
            while (i <= j) {
                /**
                 * For each iteration, we will identify a number from left side which
                 * is greater then the pivot , and also we will identify a number
                 * from right side which is less then the pivot value. Once the search
                 * is completed, then we exchange both numbers.
                 */
                while (array[i] < pivot) {
                    i++;
                }
                while (array[j] > pivot) {
                    j--;
                }
                if (i <= j) {
                    exchangeNumbers(i, j);
                    //move index to next position on both sides
                    i++;
                    j--;
                }
            }
            // call quickSort() method recursively
            if (lowerIndex < j)
                quickSort(lowerIndex, j);
            if (i < higherIndex)
                quickSort(i, higherIndex);
        }

        private void exchangeNumbers(int i, int j) {
            int temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }

        public static void main(String a[]){

            QuickSort quickSort = new QuickSort();
            int[] arr1 = {21,54,85,574,25,36,65,24,75,42};

            long startTime = System.nanoTime();//start time
            quickSort.sort(arr1);//sort the array 1
            long endTime =  System.nanoTime();//end time

            System.out.println("Original array 1\n");
            for(int i:arr1){//print original array 1
                System.out.print(i);
                System.out.print(" ");
            }

            System.out.println("\nTotal time required for unsorted array : "+(endTime-startTime)+" nanosecond");
            int[] arr2 = {1,2,3,4,5,6,7,8,9,10};
            startTime = System.nanoTime();//start time
            quickSort.sort(arr2);//sort the array 2
            endTime = System.nanoTime();//end time

            System.out.println("Original array 2\n");
            for(int i:arr2){//print original array 2
                System.out.print(i);
                System.out.print(" ");
            }

            System.out.println("\nTotal time required for sorted array : "+(endTime-startTime)+" nanosecond");
        }

}

Output

Original array 1

21 24 25 36 42 54 65 75 85 574
Total time required for unsorted array : 22805 nanosecond
Original array 2

1 2 3 4 5 6 7 8 9 10
Total time required for sorted array : 7177 nanosecond

From above output we came to conclusion that already sorted array required less time because swaping is not required only value comparisions are required.

Add a comment
Know the answer?
Add Answer to:
Hello, I need the correct answer for this please , a quick sort program -java code-...
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
  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program...

    In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count...

  • JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file...

    JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to...

  • using matlab In Class Exercises 8-Arrays For the following exercises, assume an array is already populated, your job is to write a program that traverses the array. DO NOT HARD CODE the last in...

    using matlab In Class Exercises 8-Arrays For the following exercises, assume an array is already populated, your job is to write a program that traverses the array. DO NOT HARD CODE the last index. In other words, you code for 1-4&6 should be able to handle the following 2 arrays: amp2 10 array1 [52, 63, 99, 71, 3.1] array2 - [99:-3: 45); For 5: wordArray1 wordArray2 ('number, 'arrays', 'indices', 'hello', finish' [number, 'different, 'finish? 1. Determine the sum of all...

  • Need help with java code, this is all in a method 1) 3 different arrays are...

    Need help with java code, this is all in a method 1) 3 different arrays are brought in 2) I create local array called arrayWithLargestValues which is the size of the largest array from the 3 brought in 3) I need to somehow fill the local 'arrayWithLargestValues' with the all different largest values from the 3 different arrays hopefully I made sense sample input/output int [ ] arrayWithLargestValues = new int [ ] // this has to be set to...

  • Need a quick solution to this in Java! OPTION 2: RADIX SORT TESTING Write a version...

    Need a quick solution to this in Java! OPTION 2: RADIX SORT TESTING Write a version of Radix Sort to sort an ArrayList or array of ints / Integers that you will place in a file. The integers should all be four digits long. Run the algorithm on at least 3 different files, of different sizes (i.e., each file should have a different number of integers) and have it print the results. The minimum file size should be 20 integers....

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • 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....

  • This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort...

    This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...

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