必威官网登录-betway必威官网|体育在线

热门关键词: 必威体育,betway必威官网,必威官网登录,必威app下载

空中帮衬度O(N卡塔尔,第贰个参数是数组成分个

作者: 计算机网络  发布:2019-11-12

C语言--冒泡排序

问题:

    快速排序

 

#include<stdio.h>
int main(void)
{
    int arr[10] = { 1, 2, 4, 3, 4, 3, 2, 5, 6, 7 };
    int len = sizeof(arr) / sizeof(arr[0]);
    int i = 0;
    int j = 0;
    int k = 0;
    for (i = 0; i < len; i++)
    {
        if (arr[i] < arr[i + 1]);
        for (j = 0; j < len - i -1; j++)
        {
            if (arr[j] >= arr[j + 1])
            {
                int tmp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = tmp;          
            }
        }
    }
    for (k = 0; k < len; k++)
    {
        printf("%d", arr[k]);
    }

}

 

问题: 快速排序 #includestdio.hint main(void){ int arr[10] = { 1, 2, 4, 3, 4, 3, 2, 5, 6, 7 }; int len = sizeof(arr) / sizeof(arr[0]); int i = 0; int j...

以下代码经测试,排序5000000(五千万)int型数据没有问题!

版权声明:本文为博主原创文章,未经博主允许不得转载。

第一个参数是数组首地址
第二个参数是数组元素个数

betway必威官网 1betway必威官网 2

typedef struct _BUFF { 
    int *LeftBuff; 
    int *RightBuff; 
    DWORD number;//有多少个  
    struct _BUFF *next; 
}BUFF; 
void QuickSort(int * const arr, DWORD number)//快速排序  

    int tmp; 
    int key; 
    DWORD left, right; 
    DWORD tmpLeft, tmpRight; 
    BUFF *p = NULL; 
    BUFF buff = {NULL, NULL, 0, NULL}; 
    buff.LeftBuff = (int *)malloc(1024*1024*sizeof (int)); 
    buff.RightBuff = (int *)malloc(1024*1024*sizeof (int)); 
    if ((NULL == buff.LeftBuff) || (NULL == buff.LeftBuff)) 
    { 
        free(buff.LeftBuff); 
        free(buff.LeftBuff); 
        printf("分配缓存出错!n"); 
        return; 
    } 
    buff.LeftBuff[buff.number] = 0; 
    buff.RightBuff[buff.number] = number-1; 
    buff.number++; 
    p = &buff; 
    while (buff.number/* && (NULL == buff.next)*/) 
    { 
        tmpLeft = p->LeftBuff[p->number-1]; 
        tmpRight = p->RightBuff[p->number-1]; 
        left = tmpLeft; 
        right = tmpRight; 
        key = arr[left++]; 
        do 
        { 
            while ((arr[left] < key) && (left < tmpRight)) 
            { 
                left++; 
            } 
            while ((arr[right] >= key) && (right > tmpLeft)) 
            { 
                right--; 
            } 
            if (left < right) 
            { 
                tmp = arr[left]; 
                arr[left] = arr[right]; 
                arr[right] = tmp; 
            } 
        } 
        while (left < right); 
        tmp = arr[right]; 
        arr[right] = arr[tmpLeft]; 
        arr[tmpLeft] = tmp; 
 
        if (p->number >= 1024*1024-1) 
        { 
            p->next = (BUFF *)malloc(sizeof (BUFF)); 
            if (NULL != p->next) 
            { 
                p = p->next; 
                p->LeftBuff = (int *)malloc(1024*1024*sizeof (int)); 
                p->RightBuff = (int *空中帮衬度O(N卡塔尔,第贰个参数是数组成分个数。)malloc(1024*1024*sizeof (int)); 
                p->number = 0; 
                p->next = NULL; 
            } 
            if ((0 != p->number) || (NULL == p->LeftBuff) || (NULL == p->RightBuff)) 
            { 
                printf("分配缓存出错!n"); 
                while (NULL != buff.next) 
                { 
                    p = buff.next; 
                    do 
                    { 
                        p = p->next; 
                    } 
                    while (NULL != p->next); 
                    free(p); 
                } 
                getch(); 
                return; 
            } 
        } 
        else 
        { 
            p->number--; 
        } 
 
        if (tmpLeft+1 < right) 
        { 
            p->LeftBuff[p->number] = tmpLeft; 
            p->RightBuff[p->number] = right-1; 
            p->number++; 
        } 
        if (tmpRight > left) 
        { 
            p->LeftBuff[p->number] = left; 
            p->RightBuff[p->number] = tmpRight; 
            p->number++; 
        } 
        if ((0 == p->number) && (NULL != buff.next)) 
        { 
            p = &buff; 
            while (NULL == p->next->next) 
            { 
                p = p->next; 
            } 
            free(p->next); 
            p->next = NULL; 
        } 
    } 

typedef struct _BUFF {
    int *LeftBuff;
    int *RightBuff;
    DWORD number;//有多少个
    struct _BUFF *next;
}BUFF;
void QuickSort(int * const arr, DWORD number)//快速排序
{
    int tmp;
    int key;
    DWORD left, right;
    DWORD tmpLeft, tmpRight;
    BUFF *p = NULL;
    BUFF buff = {NULL, NULL, 0, NULL};
    buff.LeftBuff = (int *)malloc(1024*1024*sizeof (int));
    buff.RightBuff = (int *)malloc(1024*1024*sizeof (int));
    if ((NULL == buff.LeftBuff) || (NULL == buff.LeftBuff))
    {
        free(buff.LeftBuff);
        free(buff.LeftBuff);
        printf("分配缓存出错!n");
        return;
    }
    buff.LeftBuff[buff.number] = 0;
    buff.RightBuff[buff.number] = number-1;
    buff.number++;
    p = &buff;
    while (buff.number/* && (NULL == buff.next)*/)
    {
        tmpLeft = p->LeftBuff[p->number-1];
        tmpRight = p->RightBuff[p->number-1];
        left = tmpLeft;
        right = tmpRight;
        key = arr[left++];
        do
        {
            while ((arr[left] < key) && (left < tmpRight))
            {
                left++;
            }
            while ((arr[right] >= key) && (right > tmpLeft))
            {
                right--;
            }
            if (left < right)
            {
                tmp = arr[left];
                arr[left] = arr[right];
                arr[right] = tmp;
            }
        }
        while (left < right);
        tmp = arr[right];
        arr[right] = arr[tmpLeft];
        arr[tmpLeft] = tmp;

计数排序:时间复杂度:O(N), 空间复杂度O(最大数-最小数)

        if (p->number >= 1024*1024-1)
        {
            p->next = (BUFF *)malloc(sizeof (BUFF));
            if (NULL != p->next)
            {
                p = p->next;
                p->LeftBuff = (int *)malloc(1024*1024*sizeof (int));
                p->RightBuff = (int *)malloc(1024*1024*sizeof (int));
                p->number = 0;
                p->next = NULL;
            }
            if ((0 != p->number) || (NULL == p->LeftBuff) || (NULL == p->RightBuff))
            {
                printf("分配缓存出错!n");
                while (NULL != buff.next)
                {
                    p = buff.next;
                    do
                    {
                        p = p->next;
                    }
                    while (NULL != p->next);
                    free(p);
                }
                getch();
                return;
            }
        }
        else
        {
            p->number--;
        }

基数排序:时间复杂度:O(N*位数),空间辅助度O(N)

        if (tmpLeft+1 < right)
        {
            p->LeftBuff[p->number] = tmpLeft;
            p->RightBuff[p->number] = right-1;
            p->number++;
        }
        if (tmpRight > left)
        {
            p->LeftBuff[p->number] = left;
            p->RightBuff[p->number] = tmpRight;
            p->number++;
        }
        if ((0 == p->number) && (NULL != buff.next))
        {
            p = &buff;
            while (NULL == p->next->next)
            {
                p = p->next;
            }
            free(p->next);
            p->next = NULL;
        }
    }
}

 

 

 

测试代码:

冒泡排序(BubbleSort)

#include <stdio.h>  
#include <conio.h>  
#include <windows.h>  
 
typedef struct _BUFF { 
    int *LeftBuff; 
    int *RightBuff; 
    DWORD number;//有多少个  
    struct _BUFF *next; 
}BUFF; 
 
void QuickSort(int * const arr, DWORD number);//快速排序  
void ExamineArr(const int * const arr, DWORD number);//检查排序结果  
 
int main(int argc, char *argv[]) 

    DWORD StartTime, EndTime; 
    DWORD i; 
    DWORD num = 50000000; 
    int *arr = NULL; 
    arr = (int *)malloc(num * sizeof (int)); 
    if (NULL == arr) 
    { 
        free(arr); 
        printf("内存分配失败,程序退出!n"); 
        getch(); 
        return -1; 
    } 
 
    StartTime = GetTickCount(); 
    for (i=0; i<num; i++) 
    { 
        *(arr+i) = rand(); 
    } 
    EndTime = GetTickCount(); 
    printf("生成%u个随机数耗时:%umsn", num, EndTime - StartTime); 
     
    StartTime = GetTickCount(); 
    QuickSort(arr, num); 
    EndTime = GetTickCount(); 
    printf("快速排序耗时:%umsn", EndTime - StartTime); 
    ExamineArr(arr, num);//检查排序结果  
 
    free(arr); 
    getch(); 
    return 0; 

 
void QuickSort(int * const arr, DWORD number)//快速排序  

    int tmp; 
    int key; 
    DWORD left, right; 
    DWORD tmpLeft, tmpRight; 
    BUFF *p = NULL; 
    BUFF buff = {NULL, NULL, 0, NULL}; 
    buff.LeftBuff = (int *)malloc(1024*1024*sizeof (int)); 
    buff.RightBuff = (int *)malloc(1024*1024*sizeof (int)); 
    if ((NULL == buff.LeftBuff) || (NULL == buff.LeftBuff)) 
    { 
        free(buff.LeftBuff); 
        free(buff.LeftBuff); 
        printf("分配缓存出错!n"); 
        return; 
    } 
    buff.LeftBuff[buff.number] = 0; 
    buff.RightBuff[buff.number] = number-1; 
    buff.number++; 
    p = &buff; 
    while (buff.number/* && (NULL == buff.next)*/) 
    { 
        tmpLeft = p->LeftBuff[p->number-1]; 
        tmpRight = p->RightBuff[p->number-1]; 
        left = tmpLeft; 
        right = tmpRight; 
        key = arr[left++]; 
        do 
        { 
            while ((arr[left] < key) && (left < tmpRight)) 
            { 
                left++; 
            } 
            while ((arr[right] >= key) && (right > tmpLeft)) 
            { 
                right--; 
            } 
            if (left < right) 
            { 
                tmp = arr[left]; 
                arr[left] = arr[right]; 
                arr[right] = tmp; 
            } 
        } 
        while (left < right); 
        tmp = arr[right]; 
        arr[right] = arr[tmpLeft]; 
        arr[betway必威官网,tmpLeft] = tmp; 
 
        if (p->number >= 1024*1024-1) 
        { 
            p->next = (BUFF *)malloc(sizeof (BUFF)); 
            if (NULL != p->next) 
            { 
                p = p->next; 
                p->LeftBuff = (int *)malloc(1024*1024*sizeof (int)); 
                p->RightBuff = (int *)malloc(1024*1024*sizeof (int)); 
                p->number = 0; 
                p->next = NULL; 
            } 
            if ((0 != p->number) || (NULL == p->LeftBuff) || (NULL == p->RightBuff)) 
            { 
                printf("分配缓存出错!n"); 
                while (NULL != buff.next) 
                { 
                    p = buff.next; 
                    do 
                    { 
                        p = p->next; 
                    } 
                    while (NULL != p->next); 
                    free(p); 
                } 
                getch(); 
                return; 
            } 
        } 
        else 
        { 
            p->number--; 
        } 
 
        if (tmpLeft+1 < right) 
        { 
            p->LeftBuff[p->number] = tmpLeft; 
            p->RightBuff[p->number] = right-1; 
            p->number++; 
        } 
        if (tmpRight > left) 
        { 
            p->LeftBuff[p->number] = left; 
            p->RightBuff[p->number] = tmpRight; 
            p->number++; 
        } 
        if ((0 == p->number) && (NULL != buff.next)) 
        { 
            p = &buff; 
            while (NULL == p->next->next) 
            { 
                p = p->next; 
            } 
            free(p->next); 
            p->next = NULL; 
        } 
    } 

 
void ExamineArr(const int * const arr, DWORD number) 

    DWORD i=0, j=1; 
    if (number <2) 
    { 
        return; 
    } 
    for (; j<number; i++,j++) 
    { 
        if (arr[i] > arr[j]) 
        { 
            printf("第%u个数大于第%u个数!n", i, j); 
            return; 
        } 
    } 

       冒泡排序(BubbleSort)是一种简单的排序算法。它重复地遍历过要排序的数列,一次比较两个元素,如果他们的顺序错误把他们交换过来。遍历数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。

betway必威官网 3

冒泡排序算法的步骤:

摘自 kevinshq的专栏

      1.比较相邻的元素。如果第一个比第二个大,就交换他们两个。

第一个参数是数组首地址 第二个参数是数组元素个数 typedef struct _BUFF { int *L...

      2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。

      3.针对所有的元素重复以上的步骤,除了最后一个。

      4.持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

选择排序(SelectSort)

       选择排序(SelecSort)是一种简单直观的排序算法。它的工作原理如下:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。

简单选择排序算法改进

       传统的简单选择排序,每趟循环只能确定一个元素排序后的定位。我们可以考虑改进为每趟循环确定两个元素(当前趟最大和最小记录)的位置,从而减少排序所需的循环次数。改进后对n个数据进行排序,最多只需进行[n/2]趟循环即可。

快速排序(QuickSort)

本文由betway必威官网发布于计算机网络,转载请注明出处:空中帮衬度O(N卡塔尔,第贰个参数是数组成分个

关键词: