-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSortingAlgorithms
More file actions
73 lines (54 loc) · 1.42 KB
/
QuickSortingAlgorithms
File metadata and controls
73 lines (54 loc) · 1.42 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
package com.bjain.bjainbooks.algo;
/**
* Created by Ajay RajPut on 20,April,2019
* Bjain Pvt. Ltd. ,
* Noida , India.
*/
public class QuickSortingAlgo {
public static void sortArray(){
int arr[]={2,18,13,54,34,23,65,56};
int start=0;
int end=arr.length-1;
System.out.print("print array before sorting");
printArray(arr);
sort(arr,start,end);
System.out.print("print array after sorting ");
printArray(arr);
}
private static void printArray(int arr[]){
for (int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println(" ");
}
private static void sort(int arr[],int s,int e){
if (s<e){
int piv=partition(arr,s,e);
sort(arr,s,piv-1);
sort(arr,piv,e);
}
}
private static void swap(int arr[],int a,int b){
int temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
private static int partition(int arr[],int s,int e){
int i=s;
int pivt=arr[e];
for (int j=i;j<e;j++){
if (arr[j]<=pivt){
swap(arr,i,j);
/* int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;*/
i++;
}
}
swap(arr,i,e);
/*int temp=arr[i];
arr[i]=arr[e];
arr[e]=temp;*/
return i;
}
}