zoukankan      html  css  js  c++  java
  • 快速排序(Quick Sort)的C语言实现

    快速排序(Quick Sort)的基本思想是通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对着两部分记录继续进行排序,以达到整个序列有序,具体步骤为
    1. 设立枢轴,将比枢轴小的记录移到低端,比枢轴大的记录移到高端,直到low=high停止
    2. 分别对枢轴低高端部分再次快速排序(即重复第1步)
    3. 重复第1、2步,直到low=high停止

    C语言实现(编译器Dev-c++5.4.0,源代码后缀.cpp)

    原创文章,转载请注明来自钢铁侠Mac博客http://www.cnblogs.com/gangtiexia

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #define MAXSIZE 6
     4 #define OK 1
     5 #define ERROR 0
     6 
     7 typedef int Status;
     8 typedef int KeyType;
     9 typedef char InfoType;//要malloc() 
    10 
    11 typedef struct{
    12     KeyType score;
    13     InfoType name[20]; 
    14 }RedType;//记录类型  
    15 
    16 typedef struct{
    17     RedType r[MAXSIZE+1];
    18     int length;
    19 }SqList;
    20 /*
    21     Name: 初始化
    22     Copyright: 
    23     Author: leo 
    24     Date: 10-12-15 16:54
    25     Description: 初始化顺序表 
    26 */
    27 
    28 Status initSqList(SqList &l){
    29     l.length=MAXSIZE;
    30     printf("%d",l.length);
    31     for(int i=1;i<=l.length;i++){
    32         printf("请输入第%d个同学的名字和分数",i);
    33         gets(l.r[i].name);
    34         scanf("%d",&(l.r[i].score));
    35         getchar();
    36         printf("
    ");
    37     }
    38     
    39     return OK;
    40 }
    41 /*
    42     Name: 部分排序 
    43     Copyright: http://www.cnblogs.com/gangtiexia
    44     Author: 钢铁侠
    45     Date: 10-12-15 17:00
    46     Description: 反复按照枢轴进行快速排序:将比枢轴大的放在一边,比枢轴小的放在另一边 
    47 */
    48 
    49 int partition(SqList &l,int low,int high){
    50     l.r[0]=l.r[low];//枢轴 
    51     while(low<high){
    52         while(low<high&&l.r[high].score>l.r[0].score) high--;
    53         l.r[low]=l.r[high];
    54         while(low<high&&l.r[low].score<l.r[0].score) {
    55         printf("
    high为%d
    ",high);
    56         low++;}
    57         l.r[high]=l.r[low];
    58     }
    59     l.r[low]=l.r[0];
    60     return low;
    61 }
    62 
    63 /*
    64     Name: 启动递归快速排序
    65     Copyright: http://www.cnblogs.com/gangtiexia
    66     Author: 钢铁侠
    67     Date: 11-12-15 14:37
    68     Description: 启动递归快速排序,并进行第一个大部分(所有记录)的快速排序 
    69 */
    70 
    71 Status QuickSort(SqList &l,int low,int high){
    72     int pivotloc;
    73     if(low<high){
    74         pivotloc=partition(l,low,high);
    75         QuickSort(l,low,pivotloc-1);
    76         QuickSort(l,pivotloc+1,high);
    77     } 
    78     return OK;
    79 }
    80 
    81 Status Traverse(SqList &l){
    82     for(int i=1;i<=MAXSIZE;i++)
    83     {
    84         printf("%s的分数为%d 
    ",l.r[i].name,l.r[i].score); 
    85     }
    86 } 
    87 
    88 int main(){
    89     
    90     SqList L;
    91     initSqList(L);
    92     printf("L.length为%d",L.length);
    93     QuickSort(L,1,L.length); 
    94     Traverse(L);
    95     return 0;
    96 }
  • 相关阅读:
    deeplearning.ai 卷积神经网络 Week 1 卷积神经网络
    deeplearning.ai 构建机器学习项目 Week 2 机器学习策略 II
    deeplearning.ai 构建机器学习项目 Week 1 机器学习策略 I
    deeplearning.ai 改善深层神经网络 week3 超参数调试、Batch Normalization和程序框架
    deeplearning.ai 改善深层神经网络 week2 优化算法
    deeplearning.ai 改善深层神经网络 week1 深度学习的实用层面
    cs231n spring 2017 lecture8 Deep Learning Networks
    cs231n spring 2017 lecture7 Training Neural Networks II
    cs231n spring 2017 lecture6 Training Neural Networks I
    cs231n spring 2017 Python/Numpy基础
  • 原文地址:https://www.cnblogs.com/gangtiexia/p/5097202.html
Copyright © 2011-2022 走看看