2024.2.28 程设能力实训 排序专题 2-排序去重(2)

本文详细描述了C语言中作者自定义的快速排序算法实现,并与冒泡排序进行对比,同时展示了如何使用去重函数处理数组。
摘要由CSDN通过智能技术生成

/*
A
20 40 32 67 40 20 89 300 400 15
*/
#include <stdio.h>

/*自己写的这个quicksort错了。(while那部分)

void quicksort(int arr[],int first,int last)
{
    int First=first,Last=last;
    int dest=arr[0],tempt=0;
    while(last>first){
        while(last>first&&arr[last]<dest){
            arr[tempt]=arr[last];
            last--;
        }
        while(last>first&&arr[first]>dest){
            arr[last]=arr[first];
            first++;
        }
        arr[first]=dest;
    }
    quicksort(arr,First,first-1);
    quicksort(arr,last+1,Last);
}*/


void quickSort(int arr[], int low, int high) {
    if (low < high) {
        int pivot = arr[low];
        int i = low;
        int j = high;

        while (i < j) {
            while (i < j && arr[j] >= pivot)
                j--;
            arr[i] = arr[j];
            while (i < j && arr[i] <= pivot)
                i++;
            arr[j] = arr[i];
        }
        arr[i] = pivot;

        quickSort(arr, low, i - 1);
        quickSort(arr, i + 1, high);
    }
}

int quchong(int arr[],int len){
    int count=0;
    for(int m=0;m<len-1;m++){
        for(int n=m+1;n<len;n++){
            if(arr[m]==arr[n]){
                for(int k=n;k<len;k++){
                    arr[k]=arr[k+1];
                }
                len--;
                n--;
                count++;
            }
        }
    }
    return count;
}

另一:这个用的是冒泡排序【方便将A和D放一起操作】,以及在去重这里,不一样的思路。

int main()
{
    char ch;
    scanf("%c",&ch);
    int num[100]={0};
    int i=0;
    while((scanf("%d",&num[i]))!=EOF) i++;
    printf("\n");
    quickSort(num,0,i-1);
    //quchong(num,i);
    int Count=quchong(num,i);
    if(ch=='A')
        for(int j=0;j<i-Count;j++) printf("%d ",num[j]);
    if(ch=='D')
        for(int j=i-1-Count;j>=0;j--) printf("%d ",num[j]);
    //printf("\n");
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值