C Language Simply Realizes Quick Sorting

Time:2019-6-29

Quick sorting is a kind of unstable sorting. Its time complexity is O (n. lgn), the worst case is O (n2), and the space complexity is O (n. lgn).
This sort method is an improvement on bubble sort. It uses divide-and-conquer mode to divide one-time sorted data into two separate parts, in which each value of one set of data is smaller than that of the other. Each trip is sorted while classifying.

The mode of each trip can be either the first or the last of the data by setting the key as the benchmark element. Here is the first example of each selected data:

Specific code implementation:


#include<stdio.h>
#define N 6
 int fun(int arr[],int low,int high)
 {
  int key;
  key=arr[low];
  while(low<high)
  {
    while(low<high && arr[high]>=key)
      high--;
    if(low<high)
      arr[low++]=arr[high];
    while(low<high && arr[low]<=key)
      low++;
    if(low<high)
      arr[high--]=arr[low];
   }
   arr[low]=key;
   return low;
 } 
void quick_sort(int arr[],int start,int end)
{
  int pos;
  if(start<end)
  {
  pos=fun(arr,start,end);
  quick_sort(arr,start,pos-1);
  quick_sort(arr,pos+1,end);
  }
}
int main()
{
  int i;
  int arr[N]={32,12,7,78,23,45};
  for(i=0;i<N;i++)
  {
    printf("%d ",arr[i]);
  }
  printf("\n");
  quick_sort(arr,0,N-1);
  for(i=0;i<N;i++)
  {
    printf("%d ",arr[i]);
  }
  return 0;
 } 

As it is the first time to write a blog, many places do not have a good habit, but also ask readers to forgive. The purpose of creating this blog is actually to impress oneself on data structure and algorithm. It is displayed in the form of a blog to facilitate one’s own access, on the other hand, in the hope of helping needy friends through one’s own meagre efforts.

I also hope that I can persevere and do such a thing seriously.

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.