From 0969e81afaa5d13a01e8c0b84f819295bd8f28c2 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Wed, 29 Sep 2021 13:10:05 +0100 Subject: [PATCH] Sorting function is complete - Pointers are not updated whilst being swapped which needs to be changed --- BinaryTree/BinaryTree/Program.cs | 2 +- QuickSort/.vscode/launch.json | 26 +++++++++++++++++ QuickSort/.vscode/tasks.json | 42 +++++++++++++++++++++++++++ QuickSort/Program.cs | 18 +++++++++++- QuickSort/QuickSort.cs | 50 +++++++++++++++++++++++++------- 5 files changed, 126 insertions(+), 12 deletions(-) create mode 100644 QuickSort/.vscode/launch.json create mode 100644 QuickSort/.vscode/tasks.json diff --git a/BinaryTree/BinaryTree/Program.cs b/BinaryTree/BinaryTree/Program.cs index 1ac1fbb..e4b5880 100644 --- a/BinaryTree/BinaryTree/Program.cs +++ b/BinaryTree/BinaryTree/Program.cs @@ -10,7 +10,7 @@ namespace BinaryTree Random random = new Random(); - for (var i = 0; i < 50; i++) + for (var i = 0; i < 10; i++) { tree.Add(random.Next(0, 100)); } diff --git a/QuickSort/.vscode/launch.json b/QuickSort/.vscode/launch.json new file mode 100644 index 0000000..1bf54cd --- /dev/null +++ b/QuickSort/.vscode/launch.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/QuickSort/.vscode/tasks.json b/QuickSort/.vscode/tasks.json new file mode 100644 index 0000000..c67ce75 --- /dev/null +++ b/QuickSort/.vscode/tasks.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/QuickSort/Program.cs b/QuickSort/Program.cs index 00ccf9c..2da4f6f 100644 --- a/QuickSort/Program.cs +++ b/QuickSort/Program.cs @@ -6,7 +6,23 @@ namespace QuickSort { 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(); } } } diff --git a/QuickSort/QuickSort.cs b/QuickSort/QuickSort.cs index 10b7136..ebde25a 100644 --- a/QuickSort/QuickSort.cs +++ b/QuickSort/QuickSort.cs @@ -7,13 +7,8 @@ namespace QuickSort { public class QuickSort { - public int[] List { get; set; } - - public QuickSort(int[] List){ - - } - public int[] Sort(){ + public int[] Sort(int[] List){ Pointer pivot = new Pointer(0, List); @@ -33,17 +28,52 @@ namespace QuickSort } if (right.Index > left.Index) { - //Swap the values of both - swap(); + //Swap the values of the pointers as well!!! + 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; } }