-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListQuickSort.java
More file actions
136 lines (133 loc) · 2.79 KB
/
ListQuickSort.java
File metadata and controls
136 lines (133 loc) · 2.79 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.util.*;
public class ListQuickSort {
//Scanner Object
Scanner read=new Scanner(System.in);
// Array of integers to hold values
private int[] arr = new int[20];
private int cmp_count; // Number of comparisons
private int mov_count; // Number of data movements
// Number of elements in array
private int n;
public ListQuickSort()
{
cmp_count = 0;
mov_count = 0;
}
private void read()
{
while (true)
{
System.out.print("Enter the number of elements in the array: ");
n = read.nextInt();
if (n > 0 && n <= 20)
{
break;
}
else if (n > 20)
{
System.out.println("\nArray can have maximum 20 elements.\n");
}
else if (n < 0)
{
System.out.println("\nEnter positive number.\n");
}
}
System.out.println("\n-----------------------");
System.out.println(" Enter array elements ");
System.out.println("-----------------------");
// Get array elements
for (int i = 0; i < n; i++)
{
System.out.print("<" + (i + 1) + "> ");
arr[i] =read.nextInt();
}
}
private void swap(int x, int y) /* Swaps the element at index x with
the element at index y */
{
int temp;
temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
public void q_sort(int low, int high)
{
int pivot, i, j;
if (low > high)
{
return;
}
/* Partition the list into two parts:
One containing elements less than or equal to pivot
Other containing elements greater than pivot */
i = low + 1;
j = high;
pivot = arr[low];
while (i <= j)
{
// Search for an element greater than pivot
while ((arr[i] <= pivot) && (i <= high))
{
i++;
cmp_count++;
}
cmp_count++;
// Search for an element less than or equal to pivot
while ((arr[j] > pivot) && (j >= low))
{
j--;
cmp_count++;
}
cmp_count++;
if (i < j) /* If the greater element is on the
left of the smaller element */
{
/* Swap the element at index i with the element
at index j */
swap(i, j);
mov_count++;
}
}
/* j now contains the index of the last element in the
sorted list. */
if (low < j)
{
swap(low, j); /* Move the pivot to its correct
position in the list */
mov_count++;
}
//Sort the list on the left of pivot using quick sort
q_sort(low, j - 1);
//Sort the list on the right of pivot using quick sort
q_sort(j + 1, high);
}
private void display()
{
System.out.println("\n-----------------------");
System.out.println(" Sorted array elements ");
System.out.println("-----------------------");
for (int j = 0; j < n; j++)
{
System.out.println(arr[j]);
}
System.out.println("Number of comparisons: " + cmp_count);
System.out.print("Number of data movements: " + mov_count);
}
private int getSize()
{
return (n);
}
public static void main(String[] args)
{
ListQuickSort myList = new ListQuickSort();
// Accept array elements
myList.read();
// Calling the sorting function
// First call to Quick Sort Algorithm
myList.q_sort(0, myList.getSize() - 1);
// Display sorted array
myList.display();
// To exit from the console
System.exit(0);
}
}