Skip to content
This repository was archived by the owner on Dec 29, 2019. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions QSort.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
class SortArray
{
public sort(int a[])
{
int temp;
for(int i=0;i<=a.length;i++)
{
for(int j=i+1;j<a.length;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(int k=0;k<a.length;k++)
{
System.out.println(a[k]);
}
}
}
public class QuickSort
{ public static void main(String args[])
{ int a[]={5,7,4,3,6,8,9,1,2,0};
QuickSort obj=new QuickSort();
for(int i=0; i<a.length; i++) System.out.print(a[i]+" "); System.out.println();
obj.quickSort(a,0,a.length-1);
for(int i=0; i<a.length; i++) System.out.print(a[i]+" "); System.out.println();
{
public static void main(String args[])
{
int arr[]={3,5,6,0,1,2,8,4,7,9};
SortArray sr=new SortArray();
sr.sort(arr);
}
void quickSort(int array[], int start, int end)
{ int i = start;
int k = end;
if (end - start >= 1)
{ int pivot = array[start];
while (k > i)
{ while (array[i] <= pivot && i <= end && k > i) i++;
while (array[k] > pivot && k >= start && k >= i) k--;
if (k > i) swap(array, i, k);
}
swap(array, start, k);
quickSort(array, start, k - 1); // quicksort the left partition
quickSort(array, k + 1, end); // quicksort the right partition
}
}//quickSort()
void swap(int array[], int index1, int index2)
{ int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}//swap()
}//class
}