SHARE
TWEET

Untitled

a guest Feb 20th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class QS : MonoBehaviour {
  6.     // This function will start the whole code off.
  7.     void Start(){
  8.         // This is to define the array and put in numbers to test the code out.
  9.         int[] Array = new int[] { 13, 10, 27, 101, 4, 25 };
  10.         //This will call the startsort function.
  11.         startsort (Array);
  12.  
  13.         // this code is for after the array is sorted and it will loop until all of
  14.         //the numbers in the array are displayed in the console.
  15.         for (int j = 0; j < Array.Length; j++) {
  16.             Debug.Log (Array [j]);
  17.         }
  18.     }
  19.        
  20.  
  21.     private void startsort(int[] Array){
  22.         //once this function is called in the start function, the quicksort function
  23.         //is called with the array, 0 being the first value in the array and then the last value of the array.
  24.         // the last value of the array is taken by getting the array length and subtracting one because arrays start with 0.
  25.         quicksort (Array, 0, (Array.Length - 1));
  26.        
  27.    
  28.     }
  29.  
  30.     private void quicksort (int[] Array, int start, int end){
  31.         // these three lines are to define the variables for the process.
  32.         //left will be the first value in the array and it is the leftmark for the quicksort.
  33.         int left = start;
  34.         // right will be the last value in the array and it is the rightmark for the quicksort.
  35.         int right = end;
  36.         // This is where the pivot is calculated. You want the pivot to be in the middle of the
  37.         //array which is why left and right are added together and then divided by 2 to get roughly in the middle.
  38.         var pivot = (Array[(left + right) / 2]);
  39.         // This is the loop that starts everything off. it will loop when the left is smaller than or equal to the right.
  40.         // As soon as left is larger than right that means that all of the numbers that are to the
  41.         //right of the pivot are smaller than the pivot and all of the numbers to the right of the pivot are larger.
  42.         while (left <= right) {
  43.             // this will compare the left to the pivot. If it is smaller than the pivot then it
  44.             //will add 1 to left meaning the next number along the array will be compared to the pivot.
  45.             while (Array [left] < pivot) {
  46.                 left++;
  47.             }
  48.             // This is the same but for the right.
  49.             // This loop will start when a number that is larger than the pivot is found on the left because
  50.             //now we are trying to find a number on the right side that is smaller than the pivot to swap them numbers.
  51.             while (Array [right] > pivot) {
  52.                 right--;
  53.             }
  54.             //This code will swap the numbers around but only if the left is smallar or equal to the right.
  55.             //If left is larger then that means that the right and left have passed each other in the array meaning the numbers shouldnt swap.
  56.             if (left <= right) {
  57.                 // temp is used to temporarily store the value that is in the array location of value left.
  58.                 // this is so that the two numbers can be swapped.
  59.                 var temp = Array [left];
  60.                 Array [left] = Array [right];
  61.                 Array [right] = temp;
  62.                 // the left and right is shifted across to the next space so that the code is looped around once more because of the first while loop.  
  63.                 left++;
  64.                 right--;
  65.             }
  66.  
  67.         }
  68.  
  69.         // Once all of the numbers on the left are smaller than the pivot and all of the numbers on the right are
  70.         //larger than the pivot we need to run the code through again for the left and right side.
  71.         //This will use the previous base code to sort the left side using different values for the left and the right.
  72.         // I use recursion here to call the funtion again but with different values for the left and right.
  73.         if (start < right) {
  74.             quicksort (Array, start, right);
  75.        
  76.         }
  77.         // once all of the left side is sorted, this code will run the base code again but for the right side.
  78.         if (left < end) {
  79.             quicksort (Array, left, end);
  80.        
  81.         }
  82.         // once all of the numbers are sorted the the for loop in the start function will output all of the numbers.
  83.     }
  84.        
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top