当前位置:网站首页>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 .
边栏推荐
- For programmers, if it hurts the most...
- Ruby时间格式转换strftime毫秒匹配格式
- Velodyne configuration command
- Latex insert picture, insert formula
- Exercise 7-8 converting strings to decimal integers (15 points)
- The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago
- The future education examination system cannot answer questions, and there is no response after clicking on the options, and the answers will not be recorded
- Vs201 solution to failure to open source file HPP (or link library file)
- Delayed message center design
- Batch distribution of SSH keys and batch execution of ansible
猜你喜欢

【Day1】 deep-learning-basics

Devop basic command

转载:等比数列的求和公式,及其推导过程

Time complexity and space complexity

uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示

今日睡眠质量记录78分

Summary of several job scheduling problems

Evolution from monomer architecture to microservice architecture
Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?

【Day2】 convolutional-neural-networks
随机推荐
Intelligent gateway helps improve industrial data acquisition and utilization
A little feeling
C language - stack
Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Es entry series - 6 document relevance and sorting
OSPF comprehensive experiment
Latex learning insertion number - list of filled dots, bars, numbers
Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment
Legion is a network penetration tool
Batch distribution of SSH keys and batch execution of ansible
MySQL case
PHP code audit 3 - system reload vulnerability
Write a program to define an array with 10 int elements, and take its position in the array as the initial value of each element.
Linked list operation can never change without its roots
What is devsecops? Definitions, processes, frameworks and best practices for 2022
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Exercise 9-4 finding books (20 points)
leetcode1229. Schedule the meeting
Dynamic address book