C++ Class 宣告

 class Sort{
private:
void Merge(int *arr, int front, int mid, int end);
int Partition(int *arr, int front, int end);
public:
void BubblesSort(int *arr, int size);
void InsertSort(int *arr, int size);
void MergeSort(int *arr, int front, int end);
void QuickSort(int *arr, int front, int end);
};

排序法實現

 int Sort::Partition(int *arr, int front, int end){
int pivot = arr[end];
int i = front;
for(int j = front; j < end; j++){
if(arr[j] < pivot){
swap(arr[j], arr[i]);
i++;
}
}
swap(arr[i], arr[end]);
return i;
} void Sort::QuickSort(int *arr, int front, int end){
if(front < end){
int pivot = Partition(arr, front, end);
QuickSort(arr, front, pivot - );
QuickSort(arr, pivot + , end);
}
} void Sort::BubblesSort(int *arr, int size){
int len = size;
for(int i = ; i < len; i++){
for(int j = i + ; j < len; j++){
if(arr[j] < arr[i])
swap(arr[i], arr[j]);
}
}
} void Sort::InsertSort(int *arr, int size)
{
int len = size;
int insert = ;
for(int i = ; i < len; i++){
insert = arr[i];
for(int j = i - ; j >= ; j--){
if(insert < arr[j])
arr[j + ] = arr[j];
else
break;
}
}
} void Sort::Merge(int *arr, int front, int mid, int end){
int i, j, k;
int n1 = mid - front + ;
int n2 = end - mid;
int LeftSub[n1];
int RightSub[n2]; /* copy array */
for(i = ; i < n1; i++)
LeftSub[i] = arr[front + i];
for(i = ; i < n2; i++)
RightSub[i] = arr[mid + i + ]; i = ; j = ; k = front;
while(i < n1 && j < n2){
if(LeftSub[i] <= RightSub[j]){
arr[k] = LeftSub[i];
i++;
}else{
arr[k] = RightSub[j];
j++;
}
k++;
}
while(i < n1){
arr[k] = LeftSub[i];
i++; k++;
}
while(j < n2){
arr[k] = RightSub[j];
j++; k++;
}
} void Sort::MergeSort(int *arr, int front, int end){
if(front < end){
int mid = (front + end) / ;
MergeSort(arr, front, mid);
MergeSort(arr, mid + , end);
Merge(arr, front, mid, end);
}
} void swap(int *a, int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}

(C/C++) 基本排序法的更多相关文章

  1. Python基础知识之排序法

    在Python开发中,我们会经常使用到排序法,排序的最简单的方法是用sort(list)函数,它接受一个列表并返回与有序的元素一个新的列表. 原始列表不被改变. a = [5, 1, 4, 3]    ...

  2. php六种基础算法:冒泡,选择,插入,快速,归并和希尔排序法

    $arr(1,43,54,62,21,66,32,78,36,76,39); 1. 冒泡排序法  *     思路分析:法如其名,就是像冒泡一样,每次从数组当中 冒一个最大的数出来.  *     比 ...

  3. Atitit.现实生活中最好使用的排序方法-----ati排序法总结

    Atitit.现实生活中最好使用的排序方法-----ati排序法总结 1. 现在的问题 1 2. 排序的类别::插入排序//交换排序//选择排序(每次最小/大排在相应的位置  )//归并排序//基数排 ...

  4. JAVA基础学习之命令行方式、配置环境变量、进制的基本转换、排序法、JAVA文档生成等(1)

    1.命令行方式 dos命令行,常见的命令: dir:列出当前目录下的文件以及文件夹 md:创建目录 rd:删除目录 cd:进入指定目录 cd..:退回到上一级目录 cd/:退回到根目录 del:删除文 ...

  5. C语言实现冒泡排序法和选择排序法代码参考

    为了易用,我编写排序函数,这和直接在主调函数中用是差不多的. 我认为选择排序法更好理解!请注意 i 和 j ,在写代码时别弄错了,不然很难找到错误! 冒泡排序法 void sort(int * ar, ...

  6. C语言 数组输出,冒泡排序法,沉底排序法,二维数组输出,输出字母列长度,从随机数组中找重复数

    #include <stdio.h> #define sum 3+4//宏定义是原封不动的使用used for test4 #include <time.h>//used fo ...

  7. CodeForces 489A SwapSort (选择排序法)

    SwapSort 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/A Description In this problem yo ...

  8. UVA 11462 Age Sort(计数排序法 优化输入输出)

    Age Sort You are given the ages (in years) of all people of a country with at least 1 year of age. Y ...

  9. (C)高级排序法

    1.快速排序法 //方法1 从大到小 #include <iostream.h> void run(int* pData,int left,int right) { int i,j; in ...

  10. c/c++ 算法之快速排序法 冒泡排序法,选择排序法,插入排序法

    本文详细叙述和实现了快速排序算法,冒泡排序 选择排序 插入排序比较简单,原理在这里不再详述,直接用代码进行了实现. 快速排序法(quicksort)是目前所公认最快的排序方法之一(视解题的对象而定), ...

随机推荐

  1. linux上的第一个c语言程序

    1.编辑源文件 输入命令如下: root@ubuntu:/home/amy# vi hello.c 文件内容如下: #include<stdio.h> int main() { print ...

  2. Oracle字符集的查看查询和Oracle字符集的设置修改(转)

    最近郁闷的字符集2014年7月31日16:32:58 本文主要讨论以下几个部分:如何查看查询oracle字符集. 修改设置字符集以及常见的oracle utf8字符集和oracle exp 字符集问题 ...

  3. JAVA基础知识总结16(IO流)

    IO流:用于处理设备上数据. 流:可以理解数据的流动,就是一个数据流.IO流最终要以对象来体现,对象都存在IO包中. 流也进行分类: 1:输入流(读)和输出流(写). 2:因为处理的数据不同,分为字节 ...

  4. python调用Go代码

    Go 1.5发布了,其中包含了一个特性:可以编译生成动态链接库,经试验,生成的.so文件可以被python加载并调用.下面举个例子: 先写一个go文件main.go: package main imp ...

  5. http头部信息解析

    HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type. 2. Accept-Chars ...

  6. selenium2 用selenium安装、加载、启用插件(一)

    一:下载 下载地址是:http://docs.seleniumhq.org/download/

  7. php扩展开发1--添加函数

    目标:便携php扩展 要求实现 输出hello word 首先用的是php7.0.3   centos7.1或者centos6.+ 1.1 RPM安装PHP rpm -Uvh https://mirr ...

  8. Windows下Memcached的安装配置方法

    1.将第一个包解压放某个盘下面,比如在c:\memcached. 2.在终端(也即cmd命令界面)下输入 'c:\memcached\memcached.exe -d install' 安装. 3.再 ...

  9. mybatis 框架 的应用之二(批量添加、实现分页查询)

    lf-driver=com.mysql.jdbc.Driver lf-url=jdbc:mysql://localhost:3306/test lf-user=LF lf-password=LF &l ...

  10. InvocationtargetException 类型转换异常

    日期类型转换不了json格式数据 json转换数据的时候可以设置某个字段不需要转换 jsonconfig=new JsonConfig() //{} 内传入不需要转换的字段 jsonconfig.se ...