SoFunction
Updated on 2025-03-06

C# implements the simplest quick sorting (you can definitely understand)

Preface

The importance of algorithms to programmers is self-evident. Today I will share with you a basic algorithm in the algorithm, which is quick sorting. As a programmer, I believe everyone is familiar with it, but it is still difficult for everyone to write it out at one time with bare hands. So there is not much nonsense, so I will briefly reduce the concept first.

Quick sorting algorithm description:

The original array L1 is selected from any reference number F (usually the first one is selected). Data less than F is placed on the left of F and is called array minList, and data greater than F is placed on the right of F and is called array maxList. So

L1=minList+F+maxList

Then do this operation on minList and maxList until the number of elements in minList and maxList is 1 or 0 stops

1. The simplest way to implement C# at present:

Now we need to implement the algorithm. It is obvious that a thought called recursion is used here. We know that algorithms are the core of programming language knowledge tools, but different programming languages ​​implement algorithms are very different (simplicity). Currently, there are many ways to quickly sort C# on the Internet. After a brief lookup, I found that it usually costs about 100 lines of code (the number of lines of code in C and C++ is smaller). After searching for a lot, I finally found one and posted it as follows:

static void QuickSort(ref List<int> nums, int left, int right)
 {
  if (left < right)
  {
  int i = left;
  int j = right;
  int middle = nums[(left + right) / 2];
  while (true)
  {
   while (i < right && nums[i] < middle) { i++; };
   while (j > 0 && nums[j] > middle) { j--; };
   if (i == j) break;
   int temp = nums[i];
   nums[i] = nums[j];
   nums[j] = temp;
   if (nums[i] == nums[j]) j--;
  }
  QuickSort(ref nums, left, i);
  QuickSort(ref nums, i + 1, right);
  }
 }

But to be honest, it is difficult to understand. If you really want to write this code in the exam room, it is difficult to ensure that you can write it correctly at once.

2. Python implementation method:

I have also been exposed to python, so when I wrote the code of this algorithm in python, I really felt that it was so simple. Students with programming experience should be able to understand the following python code.

def quicksort(array): 
 if len(array) &lt; 2:  
  return array ------Baseline conditions:An array that is empty or contains only one element is“Orderly”of 
 else:  
  pivot = array[0] ------Recursive condition
  less = [i for i in array[1:] if i &lt;= pivot] ------由所有小于基准值of元素组成of子数组  
  greater = [i for i in array[1:] if i &gt; pivot] ------由所有大于基准值of元素组成of子数组  
 return quicksort(less) + [pivot] + quicksort(greater) 
print quicksort([10, 5, 2, 3])

Just a few lines of code, it's clear and clear. The main code is that arrays can be added directly:quicksort(less) + [pivot] + quicksort(greater)

3. The easiest way to implement C# by yourself

So can we only write difficult and more code to implement it? Finally I found it, and the following is a poster of the c# code I wrote myself:

public class Extend :List&lt;int&gt;
 {
  public static Extend operator +(Extend L1, Extend L2)
  {
   (L2);
   return L1;
  }
 }

  static Extend QuickSort2(Extend nums)
  {
   if ( &lt; 2)
   {
    return nums;
   }
   else
   {
    Extend minList = new Extend();//Sets smaller than the reference number    Extend maxList = new Extend();//Sets larger than the reference number    int f = nums[0];
    for (int i = 1; i &lt; ; i++)
    {
     if (nums[i] &lt;= f) (nums[i]);
     else (nums[i]);
    }
    return QuickSort2(minList) + new Extend() { f} + QuickSort2(maxList);//Recursive, and use the + operator   }
  }

In fact, there are only two steps to achieve the same simplicity as python!

First: Create a new Extend class inherits from List<int>

Second: Rewrite the + operator

A classmate raised memory questions about the AddRange method in the Extend class, and I also replied that the algorithm is an examination of the time complexity, that is, the examination of the process. Memory consumption will definitely vary depending on the code, but it will not affect the algorithm. Of course I also made improvements to Extend, because in fact, in the final addition operation, both minList and maxList have only one element, or no elements.

public class Extend :List<int>
 {
  private static Extend k = new Extend();
  
  public static Extend operator +(Extend L1, Extend L2)
  {
   if ( == 1) (L1[0]);
   if ( == 1) (L2[0]);
   return k;
   //(L2);
   //return L1;
  }
 }

The rest are basically the same as the python code, and the code is clear and clear.

According to my observation, C# has been implemented in my way, and I am currently holding this one! Finally, I still have to complain, no wonder Python is so popular now, and the code is really simple. But as programmers, we must always remember that language is just a tool, and we are the masters of language. Understanding the ideas behind the code is the king!

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support.