最近要静下来好好未找工作做准备了,秉着好记性不如烂笔头的规律,将学习的东西记录下来,同时也希望看到的人能够指出其中的错误和可以改进之处,分享和交流让我们一起进步。
从排序算法开始看起,排序算法种类繁多,常见的包括交换排序(冒泡排序,快速排序),插入排序(直接插入排序,希尔排序),选择排序(直接选择排序,堆排序),归并排序等。本次先介绍学习交换排序的过程。
冒泡排序
从一端开始,逐个比较相邻的两个元素,发现倒序即交换位置,每一次排序,大的数据都能像水泡一样冒到序列的末尾。简单来说包括下面四个步骤:
1.比较相邻的元素,如果第一个元素比第二个元素大,那么交换两个元素的位置
2.对每一对元素都做这样的比较,这是说在一次比较中
3.对所有的元素都做上面的比较
4.知道最后没有比较为止
相关代码如下:
//C++冒泡排序代码
#include <iostream>
using namespace std;
void print (int* pData, int count) {
for (int i=0; i < count; i++)
cout << pData[i] << " ";
cout << endl;
}
void bubble (int* pData, int count) {
int temp;
int flag = 1;
int j=0;
while (flag > 0) {
flag = 0;
for (int i=0; i < count-1; i++) {
if (pData[i] > pData[i+1]) {
flag = 1;
temp = pData[i];
pData[i] = pData[i+1];
pData[i+1] = temp;
}
}
j++;
cout << "The " << j << " round:" << endl;
print (pData, count);
cout << "*******************************" << endl;
}
}
int main() {
int data[] = {3,4,1,2,5,0};
int count = 6;
bubble (data, count);
cout << "The bubble sort result:" << endl;
print (data, count);
return 0;
}
//python冒泡排序代码
def bubble(bubbleList):
listLength=len(bubbleList)
flag=1
while flag>0:
temp=0
for i in range(listLength-1):
if bubbleList[i]>bubbleList[i+1]:
temp=1
bubbleList[i],bubbleList[i+1]=bubbleList[i+1],bubbleList[i]
"""
bubbleList[i]=bubbleList[i]+bubbleList[i+1]
bubbleList[i+1]=bubbleList[i]-bubbleList[i+1]
bubbleList[i]=bubbleList[i]-bubbleList[i+1]
"""
if temp==0:
flag=-1
print bubbleList
if __name__=="__main__":
print '**********test this function************'
bubbleList1 = [3,4,1,2,5,0]
print 'The original data is:',
print bubbleList1
print 'after bubble sort:',
bubble(bubbleList1)
bubbleList2 = ['b','a','g','f','d','c','v']
print 'The original data is:',
print bubbleList2
print 'after bubble sort:',
#bubbleList2 = ['b','a','g','f','d','c','v']
bubble(bubbleList2)
快速排序
快速排序时冒泡排序的一种改进,冒泡排序中一次冒出最大的一个数,快速排序通过一趟排序将要排序的数据分成两段,其中一部分的数据比另一部分的数据都要小,然后按照这种方法对两段的数据再进行快速排序整个排序过程递归进行,最终得到数据的有序序列。
涉及到三个参数(个人理解):i,j,key
i表示从前向后(从左向右)比较的索引,j表示从后向前(从右向左)比较的索引,key表示每一轮循环中的分水岭,每个比较都是与这个key进行比较。
从前向后(从左向右)比较时找比key值大的并交换位置;
从后向前(从右向左)比较时找比key值小的并交换位置。
相关代码如下:
//C++快速排序代码
#include <iostream>
using namespace std;
void print (char* pData, int count) {
//void print (int* pData, int count) {
for (int i = 0; i < count; i++)
cout << pData[i] << " ";
cout << endl;
}
int temp;
void quickSort (char* pData, int left, int right) {
//void quickSort (int* pData, int left, int right) {
int i = left;
int j = right;
if (i >= j) {
return;
}
int key = pData[i];
while (i < j) {
while (i < j && pData[j] >= key) {
j = j-1;
}
pData[i] = pData[j];
pData[j] = key;
while (i < j && pData[i] <= key) {
i = i+1;
}
pData[j] = pData[i];
pData[i] = key;
}
quickSort(pData, left, i-1);
quickSort(pData, i+1, right);
}
int main() {
/*
int arrary1[] = {3,4,1,2,5,0};
cout << "before quick sort:";
print(arrary1,6);
quickSort(arrary1, 0, 5);
cout << "after quick sort:";
print(arrary1,6);
*/
char arrary2[] = {'b','a','g','f','d','c','v'};
cout << "before quick sort:";
print(arrary2, 7);
quickSort(arrary2, 0, 6);
cout << "after quick sort:";
print(arrary2,7);
return 0;
}
//python快速排序
def quickSort(arrary, left, right):
i = left
j = right
#位置1
#key = arrary[i]
if i >= j:
return arrary
#位置2
key = arrary[i]
while i < j:
while i < j and arrary[j] >= key:
j = j-1
arrary[i] = arrary[j]
while i < j and arrary[i] <= key:
i = i+1
arrary[j] = arrary[i]
arrary[i] = key
quickSort(arrary, left, i-1)
quickSort(arrary, j+1, right)
return arrary
if __name__ == "__main__":
arrary1 = [3,4,1,2,5,0]
left = 0
right = 5
print "before quick sort:",
print arrary1
print "after quick sort:",
quickSort(arrary1, left, right)
print arrary1
arrary2 = ['b','a','g','f','d','c','v']
left = 0
right = 6
print "before quick sort:",
print arrary2
print "after quick sort:",
quickSort(arrary2, left, right)
print arrary2
上面有一句key=arrary[i]着一语句,放在位置1和放在位置2是有很大区别的(废话!!)。放在位置1的时候,在递归的过程中会产生在某一次循环的过程中导致key值不统一,也会导致数组的索引越界的问题,在编译的时候会报出相应的错误,这个主要时针对数组比较到首位两个元素时产生的问题,具体的可以用一个简单的例子(比方说3,4,1,2,5,0为例)拿笔划一下就能发现问题了。
两者之间的区别在于冒泡排序就是一个只有右子树的递归树,递归深度未O(n),而好的快速排序会产生一个平衡的二叉树,递归深度未O(lgn).对于两者的性能分析方面还有待学习!