Sorting function is complete - Pointers are not updated whilst being swapped which needs to be changed

This commit is contained in:
Luke Else 2021-09-29 13:10:05 +01:00
parent ac83f95054
commit 0969e81afa
5 changed files with 126 additions and 12 deletions

View File

@ -10,7 +10,7 @@ namespace BinaryTree
Random random = new Random(); Random random = new Random();
for (var i = 0; i < 50; i++) for (var i = 0; i < 10; i++)
{ {
tree.Add(random.Next(0, 100)); tree.Add(random.Next(0, 100));
} }

26
QuickSort/.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net5.0/QuickSort.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}

42
QuickSort/.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/QuickSort.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/QuickSort.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/QuickSort.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}

View File

@ -6,7 +6,23 @@ namespace QuickSort
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
int[] list = {2, 5, 6, 3, 1, 9, 8};
foreach (var item in list)
{
Console.Write($" {item}");
}
QuickSort quicksort = new QuickSort();
list = quicksort.Sort(list);
foreach (var item in list)
{
Console.Write($" {item}");
}
Console.ReadLine();
} }
} }
} }

View File

@ -7,13 +7,8 @@ namespace QuickSort
{ {
public class QuickSort public class QuickSort
{ {
public int[] List { get; set; }
public QuickSort(int[] List){ public int[] Sort(int[] List){
}
public int[] Sort(){
Pointer pivot = new Pointer(0, List); Pointer pivot = new Pointer(0, List);
@ -33,17 +28,52 @@ namespace QuickSort
} }
if (right.Index > left.Index) if (right.Index > left.Index)
{ {
//Swap the values of both //Swap the values of the pointers as well!!!
swap(); List = swap(List, right, left);
} }
} //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];
}
return newList;
return new int[1]{1};
} }
public void swap(){ 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];
//Pointer2
List[pointer2.Index] = List[temp.Index];
return List;
} }
} }