0
点赞
收藏
分享

微信扫一扫

排序算法01-交换排序


一、链表冒泡排序

用两个指针即可,一个指针指向当前排序结点,另一个指针指向后续相邻结点,比较结点的数据大小并交换。内循环控制每趟排序的终止条件,外循环控制整个排序的终止条件。每一趟排序记录本次是否进行交换,若没有进行交换,则说明所有元素都有序,直接跳出外循环,否则将当前指针cur再次指向首元结点,进行下一次循环。

  • cur : 遍历链表时进行元素交换元素
  • tail : 作为cur指针遍历的边界

/**
* 时间复杂度为O(n^2)
* (n-1) + ... + 3 + 2 + 1
*/

#include<iostream>
using namespace std;

typedef struct node{
int data;
struct node *next;
}Node;

Node* BubbleSort(Node* head)
{
if(head==NULL || head->next==NULL || head->next->next==NULL)
return head;
Node* cur = head->next; //cur指向首元结点
Node* tail = NULL;
while(cur != tail){
bool isSwap = false;
//此while循环就完成一趟排序,将一个大元素沉底
while(cur->next != tail){
if(cur->data > cur->next->data){
int temp;
temp = cur->data;
cur->data = cur->next->data;
cur->next->data = temp;
isSwap = true;
}
cur = cur->next;
}// 此时一趟排序完成,cur指向最靠前的已排序元素
if(!isSwap) {
break;
}
else{
//tail指向最靠前的已排序元素
tail = cur;
cur = head->next;
}
}
return head;
}

void show(Node* head)
{
Node* temp = head->next;
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}

int main()
{
Node* head = (Node*)malloc(sizeof(Node));
head->next = NULL;
Node* tail = head;//尾插法创建
int nums[10] = {11,22,0,3,4,1,5,3,6,2};
int n = 10;
for(int i=0; i<n; i++)
{
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = nums[i];
temp->next = NULL;
tail->next = temp;
tail = temp;
}
cout<<"单链表创建完成,数据如下:";
show(head);
head = BubbleSort(head);
cout<<endl<<"排序结果如下:";
show(head);
return 0;
}

运行结果:

排序算法01-交换排序_i++

二、快速排序

/**
* 时间复杂度为O(nlogn)
*/
#include<iostream>
using namespace std;

int partition(int* nums, int low, int high){
int value = nums[low];
while(low < high){
//首先用high游标找不大于value的值
while(nums[high]>=value && low<high) high--;
nums[low] = nums[high];
//用low游标找不小于value的值
while(nums[low]<=value && low<high) low++;
nums[high] = nums[low];
}
//while循环退出后,low==high,指向value所放位置
nums[low] = value;
return low;
}

void quick_sort(int* nums, int low, int high){
//因为正常排序过程中,partition的返回值一定介于low和high,否则排序完成
//下方的low,high可能是某次返回值得到的,所以要使得返回值介于low和high之间
//就有了下面的判断条件
// 当low<high时,区间至少还有2个数据,所以需要再次排序。区间只有一个数据时,就退出
if(low < high){
int pos = partition(nums, low, high);
quick_sort(nums, low, pos-1);
quick_sort(nums, pos+1, high);
}
}

int main()
{
int nums[]={2, 3, 0, 11, 33, 45, 62, 0, 89};
int len=sizeof(nums)/sizeof(int);
quick_sort(nums, 0, len-1);
cout<<"排序后为:"<<endl;
for(int i=0; i<len; i++)
cout<<nums[i]<<" ";
return 0;
}

运行结果:

排序算法01-交换排序_结点_02

三、基数排序

1. 排序过程举例

降序排列【520,211,438,888,007,111,985,666,996,233,168】

第一趟(个位)分配结果:

排序算法01-交换排序_结点_03


第一趟收集结果:

排序算法01-交换排序_结点_04


此时得到的是个位元素递减的序列

注: 队头元素靠前,比如438在888和168前面

第二趟(十位)分配结果:

排序算法01-交换排序_结点_05


第二趟收集结果:

排序算法01-交换排序_数据_06

第三趟(百位)分配结果:

排序算法01-交换排序_数据_07

第三趟收集结果:

排序算法01-交换排序_i++_08

2. 基数排序的应用

排序算法01-交换排序_结点_09

3. 基数排序适用场景

排序算法01-交换排序_数据_10

四、桶排序

桶排序的思想很简单,就不写分析了

/**
* @author Leo
* 对于桶排序我们得知道排序元素的范围,比如当前元素范围为0 ~ 100
*/

const int range = 100;

void BucketSort(vector<int>& nums){
int* bucket = new int[range+1]();//所有元素必须初始化为0
for(int num : nums){
bucket[num]++;
}
int index = 0;
for(int i = 0; i <= range; i++){
while(bucket[i]-- > 0){
nums[index++] = i;
}
}
//for循环完成后,nums就是有序状态了
}


举报

相关推荐

0 条评论