7-1 排序 (25 分)

题意:

对给出的数字按由小到大进行排序。

思路:

之前一直没有用过堆排序,借这个题练习一下堆排序。

堆排序:

时间复杂度:

最好情况:O(nlogn)

平均情况:O(nlogn)

最坏情况:O(nlogn)

空间复杂度:O(1)

是否稳定:

代码:

 

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define MAX 1e9;
#define FRE() freopen("in.txt","r",stdin)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 1000000;
int n;
int heap[maxn];

void adjustHeap(int parent,int length){
    int temp = heap[parent];//保存父亲节点的值,因为后边会进行交换
    int child = parent*2;
    while(child<=length){//对以这个节点为根的整棵子树进行调整
        if(child<length && heap[child+1]>heap[child]){//如果是右子树的值更大的话
            child++;//将child的值,定位到右子树的下标
        }
        if(heap[child]<temp){//如果是根节点的值大于孩子节点中的最大值就结束
            break;
        }
        heap[parent] = heap[child];//进行交换
        parent = child;
        child = parent*2;
    }
    heap[parent] = temp;
}

void heapSort(){
    for(int i = n/2; i>0; i--){//从树的中间开始进行调整,因为中间位置就可以调整到整棵树的节点
        adjustHeap(i, n);
    }

    for(int i = n; i>1; i--){
        swap(heap[1], heap[i]);//每一次都会得出一个值,从小到大排序,就将他放到还没有确定好的序列的最后一位
        adjustHeap(1,i-1);
    }
    for(int i = 1; i<=n; i++){
        if(i!=1){
            printf(" ");
        }
        printf("%d",heap[i]);
    }

}

int main(){
    scanf("%d",&n);
    for(int i = 1; i<=n; i++){
        scanf("%d",&heap[i]);
    }
    heapSort();
    return 0;
}
堆排序

 

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define MAX 1e9;
#define FRE() freopen("in.txt","r",stdin)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 1000000;
int n,a[maxn],t[maxn];

void mergeArray(int st,int en,int mid,int* buf,int* temp){
    int i = st,j = mid+1;
    int k = 0;
    while(i<=mid && j<=en){//将该递归下的数组排序
        if(buf[i] < buf[j]){
            temp[k++] = buf[i++];
        }else{
            temp[k++] = buf[j++];
        }
    }
    while(i<=mid){
        temp[k++] = buf[i++];
    }
    while(j<=en){
        temp[k++] = buf[j++];
    }
    for(int p = 0; p<k; p++){//将排好序的序列再赋值到原来的位置
        buf[st+p] = temp[p];
    }
}

void mergeSort(int* buf,int st,int en,int* temp){
    if(st < en){
        int mid = (st+en)/2;
        mergeSort(buf, st, mid, temp);//向左递归分割
        mergeSort(buf, mid+1, en, temp);//向右递归分割
        mergeArray(st,en,mid,buf,temp);//合并数组
    }
}


int main(){
    scanf("%d",&n);
    for(int i = 0; i<n; i++){
        scanf("%d",&a[i]);
    }
    mergeSort(a,0,n-1,t);
    for(int i=0; i<n; i++){
        if(i){
            printf(" ");
        }
        printf("%d",a[i]);
    }
    return 0;
}
归并排序

 

转载于:https://www.cnblogs.com/sykline/p/10080393.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
选择排序是一种简单的排序算法,其基本思想是:首先在未排序的序列中找到最小(大)元素,然后将其存放到序列的起始位置;接着,再从剩余未排序的元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。 算法步骤如下: 1. 在未排序序列中找到最小(大)元素,存放到排序序列的起始位置; 2. 从剩余未排序的元素中继续寻找最小(大)元素,然后放到已排序序列的末尾; 3. 重复步骤 2,直到所有元素均排序完毕。 下面是选择排序的示例代码: ``` #include <stdio.h> void selection_sort(int arr[], int len) { int i, j, min_idx; // One by one move boundary of unsorted subarray for (i = 0; i < len-1; i++) { // Find the minimum element in unsorted array min_idx = i; for (j = i+1; j < len; j++) if (arr[j] < arr[min_idx]) min_idx = j; // Swap the found minimum element with the first element int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } } int main() { int arr[] = {64, 25, 12, 22, 11}; int n = sizeof(arr)/sizeof(arr[0]); selection_sort(arr, n); printf("Sorted array: \n"); for (int i=0; i < n; i++) printf("%d ", arr[i]); printf("\n"); return 0; } ``` 在上面的示例中,我们定义了一个 selection_sort 函数,它接受一个整数数组和数组长度作为参数,并使用选择排序算法对数组进行排序。在主函数中,我们定义了一个整数数组,然后调用 selection_sort 函数来排序该数组,并输出排序后的结果。 当我们运行这个程序时,输出结果应该为: ``` Sorted array: 11 12 22 25 64 ``` 这表明选择排序算法已成功对给定数组进行了排序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值