当前位置:网站首页>Quick sort (C language)
Quick sort (C language)
2022-07-04 10:27:00 【Lol only plays Timo】
There are many sorts , For example, Hill sort , Bubble sort , Heap sort … … There are many kinds of , Today I want to talk about the Quicksort with the lowest time complexity .
First, let's learn about the manual process of quick sorting :

With the understanding of this manual process , The code is ready , We can write an exchange function first :
void swapOnce(int *array, int start, int end) {
int i = start;
int j = end;
int temp = array[i];
if (i >= j) {
return;
}
while (i < j) {
while (i < j && array[j] > temp) {
j--;
}
if (i < j) {
array[i++] = array[j];
}
while (i < j && array[i] < temp) {
i++;
}
if (i < j) {
array[j--] = array[i];
}
}
array[i] = temp;
swapOnce(array, start, i - 1);
swapOnce(array, i + 1, end);
}
The last two lines of this exchange function are actually the idea of recursion , Because after the first overall exchange , that temp In fact, the whole array is divided into two sub arrays , The work to be done in the two sub arrays is actually the same as that of this large array , So just call this exchange function again .
The following is the array generation and the complete code of this quick sort :
#include <stdio.h>
#include <malloc.h>
#include <time.h>
int *creatArray(int minValue, int maxValue, int count);
void showArray(int *array, int count);
void swapOnce(int *array, int start, int end);
void quickSort(int *array, int count);
void quickSort(int *array, int count) {
swapOnce(array, 0, count-1);
}
void swapOnce(int *array, int start, int end) {
int i = start;
int j = end;
int temp = array[i];
if (i >= j) {
return;
}
while (i < j) {
while (i < j && array[j] > temp) {
j--;
}
if (i < j) {
array[i++] = array[j];
}
while (i < j && array[i] < temp) {
i++;
}
if (i < j) {
array[j--] = array[i];
}
}
array[i] = temp;
swapOnce(array, start, i - 1);
swapOnce(array, i + 1, end);
}
void showArray(int *array, int count) {
int index;
for(index = 0; index < count; index++) {
printf("[%d] ",array[index]);
}
printf("\n");
}
int *creatArray(int minValue, int maxValue, int count) {
int index;
int value;
int *array;
srand(time(0));
array = (int *) calloc(sizeof(int), count);
for (index = 0; index < count; index++) {
value = rand() % (maxValue - minValue + 1) + minValue;
array[index] = value;
}
return array;
}
int main() {
int *array = NULL;
int maxValue;
int minValue;
int count;
printf("please input minValue/maxValue/count:");
scanf("%d%d%d", &minValue, &maxValue, &count);
array = creatArray(minValue, maxValue, count);
printf("before sorting:\n");
showArray(array, count);
printf("after sorting:\n");
quickSort(array, count);
showArray(array, count);
free(array);
return 0;
}
Let's take a look at the running result of this function :
If you generate a function for the array in this code , Or if you have any questions about this code, you can leave a message below , Or believe me .
边栏推荐
- Seven examples to understand the storage rules of shaped data on each bit
- BGP ---- border gateway routing protocol ----- basic experiment
- leetcode1-3
- Rhcsa12
- Kotlin set operation summary
- Recursion and divide and conquer strategy
- Remove linked list elements
- Development guidance document of CMDB
- Exercise 9-5 address book sorting (20 points)
- Number of relationship models
猜你喜欢

Software sharing: the best PDF document conversion tool and PDF Suite Enterprise version sharing | with sharing

Collection of practical string functions

Dynamic address book

今日睡眠质量记录78分

【Day1】 deep-learning-basics

Jianzhi offer 04 (implemented in C language)
![[FAQ] summary of common causes and solutions of Huawei account service error 907135701](/img/73/c4ee842475f05e2e67297fcac68779.png)
[FAQ] summary of common causes and solutions of Huawei account service error 907135701

Linked list operation can never change without its roots

Reprint: summation formula of proportional series and its derivation process

MongoDB数据日期显示相差8小时 原因和解决方案
随机推荐
7-17 crawling worms (15 points)
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
如果不知道這4種緩存模式,敢說懂緩存嗎?
Hlk-w801wifi connection
OSPF comprehensive experiment
[200 opencv routines] 218 Multi line italic text watermark
Kotlin: collection use
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
Dynamic address book
Button wizard business running learning - commodity quantity, price reminder, judgment Backpack
What is devsecops? Definitions, processes, frameworks and best practices for 2022
uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示
DDL statement of MySQL Foundation
Two way process republication + routing policy
MongoDB数据日期显示相差8小时 原因和解决方案
Doris / Clickhouse / Hudi, a phased summary in June
Ruby time format conversion strftime MS matching format
Number of relationship models
按键精灵跑商学习-商品数量、价格提醒、判断背包
Laravel文档阅读笔记-How to use @auth and @guest directives in Laravel