Files
daa-lab/quickSort.java
T
2026-05-18 19:53:47 +05:30

58 lines
1.5 KiB
Java

import java.util.*;
public class quickSort{
static int partition(int[] arr, int low, int high){
int pivot = arr[low];
int i = low+1;
int j = high;
while(i<=j){
while(i<=high && arr[i]<=pivot)
i++;
while(arr[j]>pivot)
j--;
if(i<j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
int temp = arr[j];
arr[j]=arr[low];
arr[low] = temp;
return j ;
}
static void quickSort(int[] arr, int low, int high){
if(low<high){
int pi = partition(arr, low,high);
quickSort(arr, low, pi-1);
quickSort(arr, pi+1, high);
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("\nENter number of products: ");
int n = sc.nextInt();
int[] prices = new int[n];
System.out.println("ENter product prices: ");
for(int i =0;i<n;i++){
prices[i] = sc.nextInt();
}
long startTime = System.nanoTime();
quickSort(prices, 0 ,n-1);
long endTime = System.nanoTime();
System.out.println("\nSOrted prices list: ");
for(int i =0;i<n;i++){
System.out.print(prices[i]+ " ");
}
long execTime = endTime - startTime;
System.out.println("\nExecution time: "+ execTime+" nanoseconds");
sc.close();
}
}