forked from LalinduWenasara/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
59 lines (54 loc) · 1.21 KB
/
QuickSort.java
File metadata and controls
59 lines (54 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package quicksort;
import java.util.Scanner;
public class QuickSort {
void inArr(int arr[],int length)
{
Scanner sc=new Scanner(System.in);
System.out.print("Input "+length+" elements: ");
for(length--;length>=0;length--){
arr[length]=sc.nextInt();
}
}
void pArray(int arr[],int length)
{
System.out.print("Given array Elements are: ");
for(int i=0;i<length;i++)System.out.print(arr[i]+" ");
}
void swap(int arr[],int i,int j)
{
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
int part (int arr[], int low, int up)
{
int i = (low - 1);
for (int j = low; j < up; j++)
{
if (arr[j] <= arr[up])
{
i++;
swap(arr,i,j);
}
}
swap(arr,i+1,up);
return (i + 1);
}
void QuickSort(int arr[], int low, int up)
{
if (low < up)
{
int piIndex = part(arr, low, up);
QuickSort(arr, low, piIndex - 1);
QuickSort(arr, piIndex + 1, up);
}
}
public static void main(String args[])
{
QuickSort ob = new QuickSort();
int arr[] = new int[10];
ob.inArr(arr,10);
ob.QuickSort(arr,0,10-1);
ob.pArray(arr,10);
}
}