首页 > 其他分享 >C语言学习: 快速排序(递归方式)

C语言学习: 快速排序(递归方式)

时间:2023-02-02 23:33:05浏览次数:38  
标签:COUNT 递归 PLAYER int C语言 high players array 排序

 1 #include <stdio.h>
 2 #include "io_utils.h"
 3 #include <stdlib.h>
 4 #include <time.h>
 5 
 6 #define PLAYER_COUNT 50
 7 
 8 void SwapElements(int array[], int first, int second) {
 9   int temp = array[first];
10   array[first] = array[second];
11   array[second] = temp;
12 }
13 
14 void ShuffleArray(int array[], int length) {
15   srand(time(NULL));
16   //[0, RAND_MAX]
17   for (int i = length - 1; i > 0; --i) {
18     int random_number = rand() % i;
19     SwapElements(array, i, random_number);
20   }
21 }
22 
23 int Partition(int array[], int low, int high){
24   int pivot=array[high];
25   int partition=low;
26   for (int i = low; i < high; ++i) {
27     if(array[i]<pivot){
28       SwapElements(array,i,partition);
29       partition++;
30     }
31   }
32   SwapElements(array,partition,high);
33   return partition;
34 }
35 
36 void QuickSort(int array[], int low,int high){
37   if(low>=high){
38     return;
39   }
40   int partition= Partition(array,low,high);
41   QuickSort(array,low,partition-1);
42   QuickSort(array,partition+1,high);
43 }
44 
45 int main() {
46   int players[PLAYER_COUNT];
47   for (int i = 0; i < 50; ++i) {
48     players[i] = i;
49   }
50   // players : 0, 1, ..., 49
51   PRINT_INT_ARRAY(players, PLAYER_COUNT);
52   ShuffleArray(players, PLAYER_COUNT);
53   PRINT_INT_ARRAY(players, PLAYER_COUNT);
54 
55   QuickSort(players, 0, PLAYER_COUNT - 1);
56 
57   PRINT_INT_ARRAY(players, PLAYER_COUNT);
58 
59   return 0;
60 }
View Code

 

标签:COUNT,递归,PLAYER,int,C语言,high,players,array,排序
From: https://www.cnblogs.com/liumy/p/17087745.html

相关文章

  • 选择排序
    #include<iostream>#include<vector>usingnamespacestd;/***插入排序*首先找到数组中最小的元素和第一个元素进行交换*再次找到剩下的数组元素中最小的元......
  • C语言学习: 数组打乱
    1#include<stdio.h>2#include"io_utils.h"3#include<stdlib.h>4#include<time.h>56#definePLAYER_COUNT5078voidSwapElement(intarray[]......
  • C语言学习 字符串
    如果文件编码是GBK,那么他会编译成GBK编码,存储起来。   内存里面这么存储。中文是GBK编码存储,而数字和英文,是以unicode编码存储   GBK编码查询   宽......
  • c语言中获取环境变量
    #include<stdio.h>intmain(intargc,char*argv[],char*envp){/**命令行传参的时候例如:mysqlmysql-h127.0.0.1-uroot-p123*argc传入......
  • C语言++前置与后置
    1.++前置#include<stdio.h>intmain(){inti=10,j=0;/*i=i++;*//*j=i++;*/j=++i;//先对i++,再赋值给jprintf("%d%d",i,j);i=11,b=11}2.++后置#includ......
  • 递归的简单应用
    [递归]母牛的故事(C语言代码)-Dotcpp编程社区(题目在这)1#include<stdio.h>2//找到规律最重要,先尽量多写几个例子,找规律3intf(intn){4if(n<=3)returnn;......
  • 《C语言实现http下载文件》
    源码:/************************************************************Copyright(C),2016,Leon,AllRightsReserved.FileName:download.ccoding:UTF-8Descript......
  • 【C语言】memset() 内存填充块
    ......
  • c语言-----指针例子
    指针的基本应用#include<stdio.h>intmain(){ inta=100,b=200; int*p_1,*p_2=&b; p_1=&a; printf("a=%d\n",a); printf("*p_1=%d\n",*p_1); printf("b=%d......
  • 初识C语言2
       今天主要学习了常量、字符串、转义字符、注释、选择语句、循环语句、函数、数组、操作符。常量:生活中不变的量。1、字面常量(如:3.14、3等)。2、const修饰的常变量(con......