Skip to content

Instantly share code, notes, and snippets.

@MarshalX
Created October 13, 2019 11:40
Show Gist options
  • Select an option

  • Save MarshalX/7b2e84b570f50fb5a233715d7324d8b5 to your computer and use it in GitHub Desktop.

Select an option

Save MarshalX/7b2e84b570f50fb5a233715d7324d8b5 to your computer and use it in GitHub Desktop.

Revisions

  1. MarshalX created this gist Oct 13, 2019.
    56 changes: 56 additions & 0 deletions quickSort.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    using System;

    namespace cSharp {
    class Program {
    static int partition(int[] A, int start, int end) {
    int pivot = A[(start + end) / 2];
    int i = start;
    int j = end;

    while (i <= j) {
    while (A[i] < pivot)
    i++;
    while (A[j] > pivot)
    j--;
    if (i <= j) {
    int temp = A[i];
    A[i] = A[j];
    A[j] = temp;

    i++;
    j--;
    }
    }

    return i;
    }

    static void qSort(int[] A, int start, int end) {
    if (start < end) {
    int temp = partition(A, start, end);

    qSort(A, start, temp - 1);
    qSort(A, temp, end);
    }
    }

    static void print(int[] A) {
    for (int i = 0; i < A.Length; ++i)
    Console.Write("{0} ", A[i]);
    Console.WriteLine();
    }

    static void Main(string[] args) {
    Random rnd = new Random();
    const int n = 10;
    int[] A = new int[n];

    for (int i = 0; i < n; ++i)
    A[i] = rnd.Next(10);

    print(A);
    qSort(A, 0, A.Length - 1);
    print(A);
    }
    }
    }