-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
75 lines (60 loc) · 1.29 KB
/
QuickSort.java
File metadata and controls
75 lines (60 loc) · 1.29 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package sorting;
import java.util.Scanner;
public class QuickSort {
public static int[] quickSorting(int[] a,int first,int last)
{
int i,j,pivot,temp;
if(first < last)
{
pivot = first;
i = first;
j = last;
while(i<j)
{
while(a[i] <= a[pivot] && i < last)
i++;
while(a[j] > a[pivot])
j--;
if(i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[pivot];
a[pivot] = a[j];
a[j] = temp;
quickSorting(a,first,j-1);
quickSorting(a,j+1,last);
}
return a;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count,n;
System.out.println("Enter the size of Sorting Elements : ");
n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter the Elements to be Sorted : ");
for(int i = 0;i<n;i++)
arr[i] = sc.nextInt();
arr = quickSorting(arr,0,n-1);
System.out.println("The Entered Elements in Sorted Order : ");
for(int j = 0;j < n;j++)
System.out.print(arr[j]+" ");
}
}
/*
Output :
Enter the size of Sorting Elements :
5
Enter the Elements to be Sorted :
50
20
10
30
40
The Entered Elements in Sorted Order :
10 20 30 40 50
*/