• Home
  • University of the People
  • Data Structures (proctored course) CS 3303
  • Implementation of Quick Sort Algorithm

Implementation of Quick Sort Algorithm

As we know that the quick sort algorithm depends in its implementation on several elements, the most important of which are the pivot value, the partition process, and the recursion. I will show how I implemented them as follows: Pivot: First, the technique of selecting the pivot value, where, as shown in my code, I chose the value in the middle of the array and used the following equation to do this I + (r - I) / 2. Partition operation: As it appears in the code, I am performing a partition operation using a separate function, which is called partition. First, we look for any value from the left that is greater than the pivot value, then we also search from the right side for any value in the array less than the pivot value, then replace them, and we will continuously do the same thing until the two pointers meet QuickSort (main method): To make the code better readable, I wrote a helper function called swap that will do the swap operation. Finally, after doing all the necessary steps in building the quick sort algorithm, I wrote the main function of the algorithm, which takes the array and the starting and ending point of the array, and then it nds the pivot depending on the equation I mentioned earlier and then perform the partition operation, and nally, there are base condition to make sure that if the left and right sublist contain more than one element, it will re-call itself recursively with the new sublist. My asymptotic analysis of the algorithm using Big O notation is as follows: - swap: has a constant running time, which is expressed by O(1) - partition: it's going through all elements, so the running time is O(n). - quickSort: it depends (calling) on other functions, but this function without calculating partition is log(n), but sometimes it is (n) when the list is almost sorted, so after calculating all things in the quickSort method, we can conclude that the worst case is O(n?), and the average case (which occurs more frequently), is O(nlogn) The code: import java.u I.Arrays; /** * This code is the implemenng of Quick Sort Algorithm */ public class Sor ngAIgorithm { // The value that will count the exchange private sta c int count = 0; public sta c void main(String[] args) { int[] arr = {12, 9, 4, 99, 120, 1, 3, 10, 23, 45, 75, 69, 31, 88, 101, 14, 29, 91, 2, 0, 77 }; // Print the values of array before calling the quicksort System.out.printIn("The array before apply quicksort:"); System.out.printIn(Arrays.toString(arr)); // Calling the quick sort quickSort(arr, 0, arr.length - 1); // Print the value of array a er quick sort System.out.printIn("The array a er quick sort:"); System.out.printIn(Arrays.toString(arr)); System.out.printIn("The exchange is: " + count); 1 /* * Quick sort algorithm, this is func on will call * itself recursivly un I sort all the element * in the array * @param arr is the array th