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. 动态规划——最长不下降子序列(LIS)

    最长不降子序列是这样一个问题: 下面介绍动态规划的做法. 令 dp[i] 表示以 A[i] 结尾的最长不下降序列长度.这样对 A[i] 来说就会有两种可能: 如果存在 A[i] 之前的元素 A[j] ...

  2. 字节流之文件输出流FileOutputStream

    文件拷贝:

  3. 在线创建MongoDB免费集群(MangoDB Atlas)

    MongoDB Atlas是MongoDB的云服务,构建在亚马逊的AWS上,MongoDB允许用户在上面创建一个免费集群作为学习使用. 1. 注册MongoDB cloud账号: 访问www.mong ...

  4. java方法学习记录

    ---恢复内容开始--- 方法重载:两个方法有相同的名字,但参数不同,就是方法重载,且不能仅仅依据修饰符或者返回类型的不同来重载方法. 命令行参数的使用 有时候你希望运行一个程序时候再传递给它消息.这 ...

  5. 11.BETWEEN 操作符

    BETWEEN 操作符在 WHERE 子句中使用,作用是选取介于两个值之间的数据范围. BETWEEN 操作符 操作符 BETWEEN ... AND 会选取介于两个值之间的数据范围.这些值可以是数值 ...

  6. Classification and Prediction

    # coding: utf-8 # In[128]: get_ipython().magic(u'matplotlib inline') import pandas as pd from pandas ...

  7. 2.2开源的魅力:编译opencv源代码

    1.下载安装CMake 要在Windows平台下生成opencv的解决方案,需要一个名为CMake的开源软件.CMake的全称是crossplatform make.它是一个跨平台的安装(编译)工具, ...

  8. scala的futue和promise

    异步操作的有两个经典接口:Future和Promise,其中的 Future 表示一个可能还没有实际完成的异步任务的结果,针对这个结果可以添加 Callback 以便在任务执行成功或失败后做出对应的操 ...

  9. Cookie的跨域问题

    被误解的HttpCookie.Domain属性 有人说可以利用HttpCookie.Domain属性实现跨域访问,假如在A站(A.com)中写B站(B.com)的cookie,如下所示 这其实是错误的 ...

  10. Controller级别的异常处理过滤器IExceptionFilter

    1,系统自带的HandleErrorAttribute类继承自IExceptionFilter,是MVC的默认实现. 同时设置web.config <system.web> <cus ...