Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int Partition (int[] arr, int left, int right)
- {
- int pivot = arr[left];
- while(true)
- {
- while(arr[left] < pivot)
- left++;
- while(arr[right] > pivot)
- right--;
- if (arr[left] == pivot && arr[right] == pivot)
- left++;
- if(left < right)
- {
- int temp = arr[right];
- arr[right] = arr[left];
- arr[left] = temp;
- }
- else
- return right;
- }
- }
- public static void QuickSort (int[] arr, int left, int right)
- {
- if(left < right)
- {
- int pivot = Partition(arr, left, right);
- if (pivot > 1)
- {
- QuickSort(arr, left, pivot - 1);
- }
- if (pivot + 1 < right)
- {
- QuickSort(arr,pivot + 1,right);
- }
- }
- }
- public static void Main()
- {
- int[] myArr = { 9, 8, 6, 5, 4, 123, 131};
- QuickSort(myArr, 0, myArr.Length - 1);
- for(int i = 0; i < myArr.Length; i++)
- {
- Console.WriteLine(myArr[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement