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. 优于jdbc的mybatis框架入门

    1.什么是mybatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架. MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索. MyB ...

  2. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 4_Linear Regression with Multiple Variables 多变量线性回归

    Lecture 4 Linear Regression with Multiple Variables 多变量线性回归 4.1 多维特征 Multiple Features4.2 多变量梯度下降 Gr ...

  3. 自定义type

  4. Tornado之自定义session

      面向对象基础 面向对象中通过索引的方式访问对象,需要内部实现 __getitem__ .__delitem__.__setitem__方法 #!/usr/bin/env python # -*- ...

  5. 04.webservice客户端调用

    不要求所有的元素都理解,真正做开发的时候,有一些必须是要用的. 以后我们做开发的时候服务访问点的集合就一个服务的访问点.服务访问点绑定了具体的一个服务类,绑定的这个东西它本身也是一个元素.往上找,四个 ...

  6. 面试题:Java开发中的23种设计模式详解(转)

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  7. Paper: ImageNet Classification with Deep Convolutional Neural Network

    本文介绍了Alex net 在imageNet Classification 中的惊人表现,获得了ImagaNet LSVRC2012第一的好成绩,开启了卷积神经网络在cv领域的广泛应用. 1.数据集 ...

  8. plsql中查看sql执行计划

    想要优化sql语句,可以从sql执行计划入手. 在plsql客户端,提供了一个方便的按钮来查看执行计划 选中需要查看的sql语句,点击此按钮,就可以看到该条语句的执行计划了. 结果集包括描述,用户,对 ...

  9. Spring MVC:Model、View、ModelAndView

    个人理解:View为服务器上的某个文件容器,可以为JSP,FTL等动态页面文件,甚至是媒体文件等等,单单是一个文件.Model的作用是存储动态页面属性,动态页面文件即View可以在Model中获取动态 ...

  10. .net 序列化 与反序列化 Serializable

    序列化:序列化指的是 将对象 通过流的方式 保存为一个文件. 反序列化则是将该文件还原成 对象的过程. 序列化的作用:序列化可以跨语言跨平台 传输数据,将某一对象序列化成通用的文件格式在进行传输. 比 ...