0% found this document useful (0 votes)
6 views2 pages

Prog 4

The document provides a Java implementation of the Quick Sort algorithm, including methods for sorting an array, partitioning it around a pivot, and measuring the time taken for sorting. It prompts the user to input the size of the array, generates random integers, and displays the sorted array along with the elapsed time for sorting. The output example demonstrates the functionality with a sample array size of 5.

Uploaded by

Mr Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Prog 4

The document provides a Java implementation of the Quick Sort algorithm, including methods for sorting an array, partitioning it around a pivot, and measuring the time taken for sorting. It prompts the user to input the size of the array, generates random integers, and displays the sorted array along with the elapsed time for sorting. The output example demonstrates the functionality with a sample array size of 5.

Uploaded by

Mr Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

4.

QUICK SORT

import java.util.Scanner;
import java.math.*;
class quickSort
{
public static void quickSort(int[] arr, int low, int high)
{
if (low < high)
{
// Find the partition index
int pi = partition(arr, low, high);

// Recursively sort the subarrays


quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

// A Java method that partitions the array around a pivot element


public static int partition(int[] arr, int low, int high)
{
// Choose the last element as the pivot
int pivot = arr[high];
// Initialize the partition index
int i = low - 1;
// Loop through the array elements
for (int j = low; j < high; j++)
{
// If the current element is smaller than or equal to the pivot
if (arr[j] <= pivot)
{
// Increment the partition index
i++;
// Swap the current element with the element at the partition index
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// Swap the pivot element with the element at the partition index + 1
int temp = arr[i + 1];
arr[i+1] = arr[high];
arr[high] = temp;
// Return the partition index + 1
return i + 1;
}
// A Java method that measures the time taken by Quick Sort method
public static long measureTime(int[] arr)
{
// Get the current time in milliseconds before calling Quick Sort method
long startTime = System.currentTimeMillis();
// Call Quick Sort method with the array, low index and high index as
arguments
quickSort(arr, 0, arr.length-1);
// Get the current time in milliseconds after calling Quick Sort method
long endTime = System.currentTimeMillis();
// Calculate and return the elapsed time in milliseconds
System.out.println("The Starttime: " + startTime + " Milliseconds");
System.out.println("The endtime: " + endTime + " milliseconds");
return endTime - startTime;
}
// A Java main method that creates an array of n random integers and
prints the sorted array and the elapsed time
public static void main(String[] args)
{
// Create a Scanner object for user input
Scanner sc = new Scanner(System.in);
// Prompt the user to enter the value of n
System.out.println("Enter the value of n:");
// Read the user input as an integer
int n = sc.nextInt();
// Create an array of n integers
int[] arr = new int [n];
// Create a Random object for generating random numbers
// Random rand = new Random();
// Loop through the array elements
for (int i=0; i < n; i++) {
// Assign a random integer between 1000 and +1000 to each element
// arr[i] = rand.nextInt(2001) 1000;
arr[i]= (int)Math.round(Math.random() * 5000);
}
// Call measureTime method with the array as an argument and store the
result in a variable
long elapsedTime = measureTime(arr);
// Print the sorted array
System.out.println("The sorted array is:");
for (int i=0; i < n; i++)
{
System.out.print(arr[i] + “ ”);
}
System.out.println();
// Print the elapsed time in milliseconds
System.out.println("The time taken to sort is: " + elapsedTime+"
milliseconds");
}
}

OUTPUT:
Enter the value of n:
5
The Starttime: 1726396201972 Milliseconds
The endtime: 1726396201972 milliseconds
The sorted array is:
1653 2186 3155 3737 3851
The time taken to sort is: 0 milliseconds

You might also like