-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QuickSort.java Implemented QuickSort in java.
This adds QuickSort Algorithm which return the concatenation of the quicksorted list of elements that are less than or equal to the pivot, the pivot, and the quicksorted list of elements that are greater than the pivot. Closes #150
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package lecture9; | ||
|
||
public class QuickSort { | ||
|
||
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
Scanner s = new Scanner(System.in); | ||
System.out.println("Enter size: "); | ||
int n = s.nextInt(); | ||
int[] arr = new int[n]; | ||
System.out.println("Enter elements: "); | ||
for (int i = 0; i < n; i++) { | ||
arr[i] = s.nextInt(); | ||
} | ||
quicksort(arr,0,arr.length-1); | ||
for(int i=0;i<arr.length;i++){ | ||
System.out.print(arr[i]+" "); | ||
} | ||
} | ||
public static void quicksort(int arr[],int low, int high){ | ||
if(low>=high){ | ||
return; | ||
} | ||
int left=low; | ||
int right=high; | ||
int mid=(low+high)/2; | ||
int pivot=arr[mid]; | ||
while(left<=right){ | ||
while(arr[left]<pivot){ | ||
left++; | ||
} | ||
while(arr[right]>pivot){ | ||
right--; | ||
} | ||
if(left<=right){ | ||
int temp=arr[left]; | ||
arr[left]=arr[right]; | ||
arr[right]=temp; | ||
left++; | ||
right--; | ||
} | ||
} | ||
quicksort(arr,low,right); | ||
quicksort(arr,left,high); | ||
} | ||
|
||
} |
a02dd41
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment on a02dd41.
There are 38 results for the section all.space_checks. They have been shortened and will not be shown inline because they are more than 10.
Until GitMate provides an online UI to show a better overview, you can run coala locally for more details.