Created
August 26, 2017 08:59
-
-
Save summit87/76fad18e18fa0425831580802322a480 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package array; | |
| public class Swap { | |
| public static void main(String[] args) { | |
| int[] a = {-1, -2, -3, -4, 5, 6, -7, 8, 9}; | |
| quickSort(a,0,a.length-1); | |
| for (int i : a){ | |
| System.out.print(i+","); | |
| } | |
| System.out.println(); | |
| arrange(a); | |
| for (int i : a){ | |
| System.out.print(i+","); | |
| } | |
| } | |
| public static void arrange(int[] a){ | |
| int p = 0; | |
| while(a[p] < 0){ | |
| p++; | |
| } | |
| int x = p; | |
| System.out.println(p); | |
| for(int i = 0; i < p ; i+=2){ | |
| if(a[i] < 0){ | |
| if(p < a.length){ | |
| int temp = a[p]; | |
| a[p] = a[i]; | |
| a[i] = temp; | |
| p += 2; | |
| }else { | |
| p = p -1; | |
| int temp = a[p]; | |
| a[p] = a[i]; | |
| a[i] = temp; | |
| } | |
| } | |
| } | |
| } | |
| private static void quickSort(int[] a,int left,int right){ | |
| if(left < right){ | |
| pivot(a,left,right); | |
| } | |
| } | |
| private static void pivot(int[] a,int left,int right){ | |
| int i = left; | |
| int j = right; | |
| int pi = a[(left+right)/2]; | |
| while(i <= j){ | |
| while(a[i] < pi){ | |
| i++; | |
| } | |
| while (a[j] > pi){ | |
| j--; | |
| } | |
| if(i <= j){ | |
| int temp = a[i]; | |
| a[i] = a[j]; | |
| a[j] = temp; | |
| i++; | |
| j--; | |
| } | |
| } | |
| if(left < j){ | |
| pivot(a,left,j); | |
| } | |
| if(i < right) { | |
| pivot(a, i, right); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment