2021-09-28 08:59:42 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace QuickSort
|
|
|
|
{
|
|
|
|
public class QuickSort
|
|
|
|
{
|
|
|
|
|
2021-09-29 12:10:05 +00:00
|
|
|
public int[] Sort(int[] List){
|
2021-09-28 08:59:42 +00:00
|
|
|
|
|
|
|
Pointer pivot = new Pointer(0, List);
|
|
|
|
|
|
|
|
Pointer left = new Pointer(1, List);
|
|
|
|
|
|
|
|
Pointer right = new Pointer(List.Length-1, List);
|
|
|
|
|
|
|
|
while (right.Index >= left.Index)
|
|
|
|
{
|
|
|
|
while (left.Index <= right.Index && left.Value <= pivot.Value)
|
|
|
|
{
|
|
|
|
left.UpdatePointer(left.Index + 1, List);
|
|
|
|
}
|
|
|
|
while (right.Index >= left.Index & right.Value >= pivot.Value)
|
|
|
|
{
|
|
|
|
right.UpdatePointer(right.Index - 1, List);
|
|
|
|
}
|
|
|
|
if (right.Index > left.Index)
|
|
|
|
{
|
2021-09-29 12:10:05 +00:00
|
|
|
//Swap the values of the pointers as well!!!
|
|
|
|
List = swap(List, right, left);
|
2021-09-28 08:59:42 +00:00
|
|
|
}
|
2021-09-29 12:10:05 +00:00
|
|
|
//Might be
|
|
|
|
List = swap(List, pivot, right);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
int[] leftHalf = new int[right.Index];
|
|
|
|
for (var i = 0; i <= right.Index; i++)
|
|
|
|
{
|
|
|
|
leftHalf[i] = List[i];
|
|
|
|
}
|
|
|
|
leftHalf = Sort(leftHalf);
|
|
|
|
|
|
|
|
int[] rightHalf = new int[List.Length - right.Index];
|
|
|
|
for (var i = 0; i <= right.Index; i++)
|
|
|
|
{
|
|
|
|
rightHalf[i] = List[i];
|
|
|
|
}
|
|
|
|
rightHalf = Sort(rightHalf);
|
|
|
|
|
|
|
|
int[] newList = new int[leftHalf.Length + rightHalf.Length];
|
|
|
|
for (var i = 0; i < leftHalf.Length; i++)
|
|
|
|
{
|
|
|
|
newList[i] = leftHalf[i];
|
|
|
|
}
|
|
|
|
for (var i = 0; i < rightHalf.Length; i++)
|
|
|
|
{
|
|
|
|
newList[i + leftHalf.Length] = rightHalf[i];
|
2021-09-28 08:59:42 +00:00
|
|
|
}
|
2021-09-29 12:10:05 +00:00
|
|
|
return newList;
|
2021-09-28 08:59:42 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-29 12:10:05 +00:00
|
|
|
public int[] swap(int[] List, Pointer pointer1, Pointer pointer2){
|
|
|
|
Pointer temp = pointer1;
|
|
|
|
//Assign both the value and index
|
|
|
|
//Pointer1
|
|
|
|
List[pointer1.Index] = List[pointer2.Index];
|
2021-09-28 08:59:42 +00:00
|
|
|
|
2021-09-29 12:10:05 +00:00
|
|
|
//Pointer2
|
|
|
|
List[pointer2.Index] = List[temp.Index];
|
|
|
|
return List;
|
2021-09-28 08:59:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Pointer{
|
|
|
|
public int Index { get; set; }
|
|
|
|
public int Value { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public Pointer(int index, int[] list){
|
|
|
|
this.Index = index;
|
|
|
|
this.Value = list[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdatePointer(int index, int[] list){
|
|
|
|
this.Index = index;
|
|
|
|
this.Value = list[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|