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

QuickSort Algorithm Implementation

University of the People Instructor: Rupa Sarda Data Structures - 3303, Week 6 March 13, 2024 About quickSortExample code: Sorting algorithm with an implementation of the quickSort uses an array of integers of at least 21 different items. The items in the list are in random order. This algorithm sorts the list using the sorting algorithm and keeps track of the number of exchanges that are required to sort the list. This code demonstrates the Quicksort algorithm for sorting an array of integers and prints the unsorted and sorted arrays. The quickSort method recursively partitions and sorts the array, and the partition method helps in finding the correct position for the pivot element. import jeliot.io .*; public class QuickSortExample { public static void quickSort(int array[], int low, int high) { if (low < high) { int partitionIndex = partition(array, low, high); quickSort(array, low, partitionIndex - 1); quickSort(array, partitionIndex + 1, high); } } public static int partition(int array[], int low, int high) { int pivot = array[high]; int i = low - 1; for (int j = low; j < high; j++) { if (array[j] < pivot) { i++; // Swap array[i] and array[j] int temp = array[i]; array[i] = array[j]; array[j] = temp; } } // Swap array[i + 1] and array[high] (pivot) int temp = array[i + 1]; array[i + 1] = array[high]; array[high] = temp; return i + 1; } public static void main(String a[]) { int[] arr1 = {12, 9, 4, 99, 120, 1, 3, 10, 23, 45, 75, 69, 31, 88, 101, 14, 29, 91, 2, 0, 77}; System.out.println("Printing unsorted array before Quicksort"); for (int i = 0; i < arr1.length; i++) { System.out.print(arr1[i] +""); }