Chinaunix首页 | 论坛 | 博客
  • 博客访问: 284164
  • 博文数量: 82
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 874
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-21 09:58
个人简介

traveling in cumputer science!!

文章分类

全部博文(82)

文章存档

2016年(13)

2015年(69)

我的朋友

分类: C/C++

2016-02-20 17:27:31

1.快排序理论:
快速排序是一种平均时间性能非常好的方法。一般排序算法,每次循环之后只能减少一个数据量因此复杂度是O(N2),而快排序是选定基准元素,一般取第一个,每次安排完一个数据后还能将其余数据分成一组比它大和一组比它小的两组数据,按此方法,再依次分别对分好类的数据进行排序操作,下面以一组数据举例。
int arrIn[] = {36,34,67,44,8,6,9,45,87}
以第一项36为基准元素,
36 [ ], 34, 67, 44, 8, 6, 9, 45, 87
                                                 <--
36 [ 9], 34, 67, 44, 8, 6, [ ], 45, 87
            -->
36 [ 9], 34, [ ], 44, 8, 6, [ 67], 45, 87
                                        <--
36 [ 9], 34, [6 ], 44, 8, [ ], [ 67], 45, 87
                    -->
36 [ 9], 34, [6 ], [ ], 8, [ 44], [ 67], 45, 87
                                    <--
36 [ 9], 34, [6 ], [8 ], [ ], [ 44], [ 67], 45, 87
                                      --><--
左右指针相遇完成一次排序,将基准元素放到空余位置

第一次排序后的list为:
9 ,34 ,6 ,8 ,3644 ,67 ,45 ,87
第二次从左半部分开始:
8 ,6 ,9 ,34
第三次从左半部开始

6 8 ,9 ,34
第四次从36的右半部开始,类似上面操作
。。。
最终排序结果
6 ,8 ,9 ,34 ,36 ,44 ,45 ,67 ,87

2.基于堆栈实现的快排序代码:
堆栈操作实现:

点击(此处)折叠或打开

  1. #ifndef _CPPSTACK_H_
  2. #define _CPPSTACK_H_
  3. /****** include *******/
  4. #include <iostream>
  5. #include <stdio.h>
  6. /****** define *********/
  7. /****** variable ********/
  8. struct Item
  9. {
  10.     int data;
  11.     Item* p_next;
  12. };

  13. class CPPStack
  14. {
  15. public:
  16.     CPPStack();
  17.     CPPStack(const CPPStack & otherStack);//constructor reset
  18.     CPPStack operator = (const CPPStack & otherStack);//'=' reset
  19.     void push(int x);//in stack
  20.     bool pop(int &x);//out stack
  21.     bool isEmpty();
  22.     void clearStack();
  23.     int sizeStack();
  24.     void printStack();
  25.     ~CPPStack();
  26. public:
  27.     Item* p_top;//stack top
  28. };
  29. /****** function *******/
  30. #endif // _CPPSTACK_H_

点击(此处)折叠或打开

  1. /*****************************
  2. filename: CPPStack.cpp
  3. description: realize stack operate in CPP
  4.                 like, stack struct; push; pop; isEmpty; clear;
  5.                 size; print
  6. author: warrior mail:15933533880@163.com
  7. date:2016-1-31
  8. log:
  9. *****************************/

  10. /****** include *******/
  11. #include "CPPStack.h"
  12. using namespace std;
  13. /****** define *********/
  14. /****** variable ********/
  15. /****** function *******/
  16. /****************
  17. description: stack operate class constructor
  18. input:
  19.     none
  20. output:
  21.     none
  22. ****************/
  23. CPPStack::CPPStack()
  24. {
  25.     p_top = NULL;
  26. }
  27. CPPStack::CPPStack(const CPPStack & otherStack)
  28. {
  29.     *this = otherStack;
  30. }

  31. /****************
  32. description: reset operator '=' for stack
  33. input:
  34.     const CPPStack otherStack : the right value of '='
  35. output:
  36.     CPPStack
  37. ****************/
  38. CPPStack CPPStack::operator=(const CPPStack & otherStack)
  39. {
  40.     CPPStack temporaryStack;
  41.     Item* copyElement = otherStack.p_top;

  42.     while(copyElement)
  43.     {
  44.         temporaryStack.push(copyElement->data);
  45.         copyElement = copyElement->p_next;
  46.     }

  47.     copyElement = temporaryStack.p_top;

  48.     while(copyElement)
  49.     {
  50.         this->push(copyElement->data);
  51.         copyElement = copyElement->p_next;
  52.     }
  53.     return *this;
  54. }

  55. /****************
  56. description: insert one element into stack
  57. input:
  58.     const int x : the value to be insert into stack
  59. output:
  60.     none
  61. ****************/
  62. void CPPStack::push(const int x)
  63. {
  64.     Item *temporaryElement;
  65.     temporaryElement = new Item;
  66.     temporaryElement->data = x;
  67.     if(this->p_top != NULL)
  68.     {
  69.         temporaryElement->p_next = this->p_top;
  70.         this->p_top = temporaryElement;
  71.     }
  72.     else
  73.     {
  74.         this->p_top = temporaryElement;
  75.         this->p_top->p_next = NULL;
  76.     }
  77.     printf("push stack data: %d \n", temporaryElement->data);
  78. }
  79. /****************
  80. description: get one element from stack
  81. input:
  82.     const int & x : storage the value get from stack into x
  83. output:
  84.     ture:success
  85.     false:error
  86. ****************/
  87. bool CPPStack::pop(int &x)
  88. {
  89.     if(this->p_top != NULL)
  90.     {

  91.         x = this->p_top->data;
  92.         this->p_top = this->p_top->p_next;
  93.         printf("pop stack data: %d \n", x);
  94.         return true;
  95.     }
  96.     else
  97.     {
  98.         return false;
  99.     }
  100. }
  101. /****************
  102. description: judge whether stack empty
  103. input:
  104.     none
  105. output:
  106.     ture:not empty
  107.     false:empty
  108. ****************/
  109. bool CPPStack::isEmpty()
  110. {
  111.     if(this->p_top != NULL)
  112.     {return true;}
  113.     else
  114.     {return false;}
  115. }
  116. /****************
  117. description: clear all element of stack
  118. input:
  119.     none
  120. output:
  121.     none
  122. ****************/
  123. void CPPStack::clearStack()
  124. {
  125.     Item* tempElement;
  126.     while(this->p_top)
  127.     {
  128.         tempElement = this->p_top;
  129.         this->p_top = this->p_top->p_next;
  130.         delete tempElement;
  131.     }
  132. }
  133. /****************
  134. description: test the size of stack
  135. input:
  136.     none
  137. output:
  138.     int lenght
  139. ****************/
  140. int CPPStack::sizeStack()
  141. {
  142.     int counter = 0;
  143.     Item* tempElement;
  144.     tempElement = this->p_top;
  145.     while(tempElement)
  146.     {
  147.         counter++;
  148.         tempElement = tempElement->p_next;
  149.     }
  150.     return counter;
  151. }
  152. /****************
  153. description: print all elements of stack
  154. input:
  155.     none
  156. output:
  157.     none
  158. ****************/
  159. void CPPStack::printStack()
  160. {
  161.     Item* tempElement = this->p_top;
  162.     while(tempElement)
  163.     {
  164.         printf(" %d\n",tempElement->data);
  165.         tempElement = tempElement->p_next;
  166.     }
  167. }

  168. CPPStack::~CPPStack()
  169. {
  170.     Item* tempElement;
  171.     while(this->p_top)
  172.     {
  173.         tempElement = this->p_top;
  174.         this->p_top = this->p_top->p_next;
  175.         delete tempElement;
  176.     }
  177. }

快速排序算法实现:

点击(此处)折叠或打开

  1. /*****************************
  2. filename: quickSort.cpp
  3. description: permutation a list of integers in O(1) and no recursion
  4. author: warrior mail:15933533880@163.com
  5. date:2016-1-30
  6. log:
  7. *****************************/

  8. /****** include *******/
  9. #include "quickSort.h"
  10. #include "CPPStack.h"
  11. using namespace std;
  12. /****** define *********/
  13. /****** variable ********/

  14. /****** function *******/
  15. /****************
  16. description: permutation a list of integers in O(1) and no recursion
  17. input:
  18.     int* arrIN : the in put interger array
  19.     int arrLength : length of array "arrIN"
  20. output:
  21.     0:run error
  22.     1:run success
  23. ****************/
  24. bool quickSort(int* arrIN, int arrLength)
  25. {
  26.     int supportElement;
  27.     int emptySign = 0;
  28.     int lowRecord = 0;
  29.     int highRecord = 0;
  30.     int temp;
  31.     int lowSign = 0;
  32.     int highSign = arrLength-1;
  33.     CPPStack quickSortStack;
  34.     if(arrIN==NULL)
  35.     {return 0;}

  36.     quickSortStack.push(arrLength-1);
  37.     quickSortStack.push(0);

  38.     while(quickSortStack.isEmpty())
  39.     {
  40.         quickSortStack.pop(lowSign);
  41.         quickSortStack.pop(highSign);

  42.         lowRecord = lowSign;
  43.         highRecord = highSign;
  44.         emptySign = lowSign;
  45.         supportElement = arrIN[emptySign];

  46.         if(highSign-lowSign>1)
  47.         {
  48.             while(lowSign<highSign)
  49.             {
  50.                 //search from high
  51.                 while((lowSign<highSign) && (supportElement<=arrIN[highSign]))
  52.                 {
  53.                     highSign--;
  54.                     //printf(", %d\n", highSign);
  55.                 }
  56.                 if(lowSign<highSign)
  57.                 {
  58.                     arrIN[emptySign] = arrIN[highSign];
  59.                     emptySign = highSign;
  60.                 }
  61.                 //search from low
  62.                 while((lowSign<highSign) && (supportElement>=arrIN[lowSign]))
  63.                 {
  64.                     lowSign++;
  65.                     //printf(", %d\n", lowSign);
  66.                 }
  67.                 if(lowSign<highSign)
  68.                 {
  69.                     arrIN[emptySign] = arrIN[lowSign];
  70.                     emptySign = lowSign;
  71.                 }
  72.             }
  73.             arrIN[emptySign] = supportElement;
  74.             printf("the sorting line:\n");
  75.             for(int i = 0; i<arrLength; i++)
  76.             {
  77.                 printf(", %d", arrIN[i]);
  78.             }
  79.             printf("\n");
  80.             //push high
  81.             quickSortStack.push(highRecord);
  82.             if(lowSign == highRecord)
  83.                 quickSortStack.push(lowSign);
  84.             else
  85.                 quickSortStack.push(lowSign+1);
  86.             //push low
  87.             if(lowSign == lowRecord)
  88.                 quickSortStack.push(lowSign);
  89.             else
  90.                 quickSortStack.push(lowSign-1);
  91.             quickSortStack.push(lowRecord);

  92.         }
  93.         else
  94.         {
  95.             if(arrIN[lowSign]>arrIN[highSign])
  96.             {
  97.                 temp = arrIN[highSign];
  98.                 arrIN[highSign] = arrIN[lowSign];
  99.                 arrIN[lowSign] = temp;
  100.             }
  101.         }
  102.     }
  103.     printf("the sorted line:\n");
  104.     for(int i = 0; i<arrLength; i++)
  105.     {
  106.         printf(", %d", arrIN[i]);
  107.     }
  108.     printf("\n");
  109. }

点击(此处)折叠或打开

  1. #ifndef _QUICKSORT_H_
  2. #define _QUICKSORT_H_
  3. /****** include *******/
  4. #include <iostream>
  5. #include <stdio.h>
  6. /****** define *********/
  7. /****** variable ********/

  8. /****** function *******/
  9. bool quickSort(int* arrIN, int arrLength);
  10. #endif // _QUICKSORT_H_
主函数:

点击(此处)折叠或打开

  1. #include "CPPStack.h"
  2. #include "quickSort.h"


  3. using namespace std;

  4. int main()
  5. {
  6.     int arrIn[] = {36,34,67,44,8,6,9,45,87};
  7.     quickSort(arrIn, sizeof(arrIn)/sizeof(int));

  8.     return 0;
  9. }


3.总结
这次实现代码过程中遇到的问题
(1)遇到“segmentation fault”错误,段错误,经过查资料,知道是指针误操作导致,错误原因:程序中指针没有初始化就进行相应操作
(2)压栈操作压入数据异常,此处也属于指针操作错误,指针操作前应先初始化,C++ 用new关键字, C里面用malloc函数
            使用实例:
            new:
            int *p = new int(10);
            注意使用结束时使用delete释放:
            delete p;
            malloc:
            void *malloc(long NumBytes)
            char *charList = (char *)malloc(100 * sizeof(char));
            注意使用结束时,使用free释放:
            free(charList);
(3)快速排序是一种不稳定排序方法


阅读(2752) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~