算法导论:快速排序和插入排序

代码实现

 1 #ifndef _SORT_H
 2 #define _SORT_H
 3
 4 // goal: quicksort and insertsort
 5 // time: 12/2/2014
 6 // author: zrss
 7 // reference: introduction to algorithms
 8
 9 class Sort {
10 public:
11     void quickSort(int A[], int p, int r);
12     void insertSort(int A[], int p, int r);
13
14 private:
15     int partition(int A[], int p, int r); // for quickSort
16 };
17
18 int Sort::partition(int A[], int p, int r) {
19     int x = A[r]; // use A[r] as pivot node
20
21     int i = p - 1; // point to left part
22
23     // divide A array to three part
24     // A[p - i] <= x
25     // A[(i + 1) - j] > x
26     // A[(j + 1) - r) unknown
27     for (int j = p; j < r; ++j) {
28         if (A[j] <= x) {
29             ++i;
30             if (i != j) { // swap A[i] and A[j]
31                 int temp = A[i];
32                 A[i] = A[j];
33                 A[j] = temp;
34             }
35         }
36     }
37
38     // exchange A[i + 1] and A[r]
39     A[r] = A[i + 1];
40     A[i + 1] = x;
41
42     return (i + 1);
43 }
44
45 void Sort::quickSort(int A[], int p, int r) {
46     if (p < r) {
47         int q = partition(A, p, r);
48         quickSort(A, p, q - 1);
49         quickSort(A, q + 1, r);
50     }
51 }
52
53 void Sort::insertSort(int A[], int p, int r) {
54     for (int j = p + 1; j <= r; ++j) {
55         int key = A[j];
56         int i = j - 1;
57         while (i >= p && A[i] > key) { // move backward
58             A[i + 1] = A[i];
59             --i;
60         }
61
62         if (i + 1 != j) { // insert A[j] to right position
63             A[i + 1] = key;
64         }
65     }
66 }
67
68
69 #endif

插入排序与快速排序运行时间比较

随机产生100,000个int测试数据

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include "windows.h"
 4 #include "sort.h"
 5
 6 int main(void) {
 7     const int length = 100000;
 8     int number[length];
 9
10     for (int i = 0; i < length; ++i) {
11         number[i] = rand();
12     }
13
14     Sort sort;
15
16     DWORD st = GetTickCount();
17
18     sort.insertSort(number, 0, length - 1);
19     //sort.quickSort(number, 0, length - 1);
20
21     DWORD ed = GetTickCount();
22
23     printf("InsertSort use time: %dms\n", ed - st);
24     //printf("QuickSort use time: %dms\n", ed - st);
25
26     system("pause");
27     return 0;
28 }

InsertSort use time: 2200ms

QuickSort use time: 0ms

系统信息

System: Windows 7 professional Service Pack 1

CPU: Intel(R) Pentium(R) Dual CPU E2220 @ 2.40GHz 2.40GHz

Memory: 3.00 GB

结论

在输入为100, 000数量级上,且输入的数据为随机值时,快速排序的效率优于插入排序

时间: 2024-08-06 15:34:17

算法导论:快速排序和插入排序的相关文章

算法导论—排序之插入排序

void insertion_sort(vector<int> &num){ for(int i = 1; i < num.size(); i++){ int j = i-1; int val = num[i]; while(j>=0 && num[j] >= val){ num[j+1] = num[j]; j--; } num[j+1] = val; } } 每次迭代时,将num[i] 作为key值,且前子数组[0,i-1] 构成已排好序,每次与左

算法导论学习之插入排序+合并排序

最近准备花时间把算法导论详细的看一遍,强化一下算法和数据结构的基础,将一些总结性的东西写到博客上去. 一.插入排序 算法思想:如果一个数组A,从A[1–n-1]都是有序的,然后我们将A[n]插入到A[1–n-1]的某个合适的位置上去那么就可以保证A[1–n]都是有序的.这就是插入排序的思想:具体实现的时候我们将数组的第一个元素看出有序,然后从第二个元素开始按照上面的步骤进行插入操作,直到插入最后一个元素,然后整个数组都是有序的了. 时间复杂度分析:代码中有两重for循环,很容易看出时间复杂度是n

算法导论学习 之 插入排序

刚刚开始系统学习算法导论和c++,每次学习的算法都用c++ 实现一边,加深理解,方便回顾. 先从最简单的插入排序开始吧: code: #include<iostream> #include<vector> #include<map> using namespace std; void Insertion_Sort(int * a,int length) { int i,j,key; for(i = 1;i < length;i ++){ key = a[i]; j

算法导论-快速排序

一.快速排序的描述 快速排序是基于分治策略的.对一个子数组A[p…r]快速排序的分治过程的三个步骤为: 1.分解 数组A[p…r]被划分成两个(可能空)子数组A[p…q-1]和A[q+1…r],使得A[p…q-1]中的每个元素都小于等于A[q],且小于等于A[q+1…r]中的元素.下标q也在这个划分过程中进行计算. 2.解决 通过递归调用快速排序,对子数组A[p…q-1]和A[q+1…r]排序. 3.合并 因为两个字数组就是原地排序的,将它们的合并不需要操作:整个数组A[p…r]已排序. 快速排

算法导论笔记1 - 插入排序 vs 归并排序

import random import time __author__ = 'Administrator' LENGTH = 3000 base = [] for i in range(0, LENGTH): base.append(random.randint(0, LENGTH)) def ins_sort(array): for border in range(1, len(array)): j = border - 1 key = array[border] while j >= 0

算法导论2.1 插入排序

插入排序 // insertion_sort.h not with template #include <iostream> #include <stdint.h> // INSERTION-SORT(A) // for j = 2 to A.length // key = A[j] // // Insert A[j] into sorted sequence A[1..j - 1]. // i = j - 1 // while i > 0 and a[i] > key

Algorithm: quick sort implemented in python 算法导论 快速排序

1 import random 2 3 def partition(A, lo, hi): 4 pivot_index = random.randint(lo, hi) 5 pivot = A[pivot_index] 6 A[pivot_index], A[hi] = A[hi], A[pivot_index] 7 store_index = lo 8 for i in range(lo, hi): 9 if A[i] < pivot: 10 A[i], A[store_index] = A[

算法导论 第2章

本章主要是算法知识的基础讲解,介绍了循环不变式,几个简单的排序算法,递归分治算法等内容. 1.循环不变式 循环不变式主要用来说明算法的正确性,那么什么是循环不变式呢,其实就是在循环过程中,一些元素数据必须保持的一些性质,例如在插入排序中,数组为A,必须保证三个性质: (1) 初始化:在循环开始之前,循环不变式是成立的,即:A[0]是有序的,A[1...n-1]是无序的. (2) 保持:在循环的某一次迭代开始之前,循环不变式是成立的,那么在此次迭代结束后依然应该是成立的,即:A[0...i]是有序

算法导论第7章___快速排序

快速排序本质上是插入排序,但是它在这个基础上增强了算法. 下面我们来分析一下快速排序: 有了前面的分析基础,我们在来看排序算法也就容易多了. public class Quick_Sort { private void quick_Sort(int []A,int left,int right){ if(left<right){ //划区比较,这个partition 第一次!得到的就是我们刚才说的2. int partition=partition(A, left, right); //实现第一

《算法导论》 — Chapter 7 快速排序

序 快速排序(QuickSort)也是一种排序算法,对包含n个数组的输入数组,最坏情况运行时间为O(n^2).虽然这个最坏情况运行时间比较差,但是快速排序通常是用于排序的最佳实用选择,这是因为其平均性能相当好,期望的运行时间为O(nlgn),且O(nlgn)中隐含的常数因子很小,另外它还能够进行就地排序在虚拟环境中也能很好的工作. 原理 快速排序也和合并排序一样,基于分治法,分为分解.解决.合并三个步骤: 分解:数组array[low-high]被分为两个(可能空)子数组array[low-te