冒泡,选择,归并排序

冒泡,选择,归并排序_第1张图片

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "conio.h"
void swap(int *a, int *b);
void selectsort(int a[]);
void paopaosort(int a[]);
void guibsort(int(*p)[8],int m,int n);
int main() {
    int a[10] = {10,3,6,2,4,8,7,9,1,5};
    int b[10] = {10,3,6,2,4,8,7,9,1,5 };
    int c[8] = {10,3,6,2,4,8,7,9};
    printf("This is paopao sort\n");
    paopaosort(a);
    printf("This is select sort");
    printf("\n");
    selectsort(b);
    printf("This is guib sort\n");
    guibsort(&c,8,0);
    for (int i = 0; i < 8; i++) {   
        printf("%d\t", c[i]);
    }
    return 0;
}
void paopaosort(int a[]) {
    for (int j = 0; j < 10; j++)
    {
        for (int i = 0; i < 10 - j-1; i++) 
        {
            if (a[i] > a[i + 1]) {
                swap(&a[i], &a[i + 1]);
            }   
        }
        printf("#%d line is:\n", j);
        for (int k = 0; k < 10; k++) 
        {
            printf("%d\t", a[k]);
        }
        printf("\n");
    }
}
void selectsort(int a[]) {

    for (int j = 0; j < 10; j++) 
    {
        int min = 100;
        int u = 0;
        for (int i = j; i < 10; i++) 
        {
            if(a[i]<min)
            {
                min = a[i];
                u = i;
            }

        }
        swap(&a[u], &a[j]);
        printf("#%d line is:\n", j);
        for (int k = 0; k < 10; k++)
        {
            printf("%d\t", a[k]);
        }
        printf("\n");
    }
}
void guibsort(int(*p)[8],int m,int n) { 
    if (m==1)
    {
        return;
    }
    else 
    {
        guibsort(p,m/2,n); guibsort(p,m/2,n+m/2); //printf("\nm is scal=%d,n is numbegin=%d\n", m, n); int kk[8]; int q = 0; int p1 = n + q; int p2 = n + m / 2 + q;
        for (int q = p1; q < n+m; q++) { kk[q] = *(*p + q); //printf("%d\t", kk[q]); } for (q = 0; q < m; q++) { if (p2>=n+m&&p1<m / 2+n) {
                *(*p + n + q) = kk[p1];
                //printf("sort %d %d\n", q, *(*p + n + q));
                p1++;
                break;
            }
            else if (p1 > n+m / 2 && p2 < n+m) {
                *(*p + n + q) = kk[p2];
                //printf("sort %d %d\n", q, *(*p + n + q));
                p2++;
                break;
            }
            else if (p1 >n+ m / 2 && p2 > n+m) {
                break;
            }
            else {
                for (; p1 < n+m / 2 && p2 <n+ m;)
                {
                    //printf("aaaaa%d\tbbbb%d\t", kk[p1], kk[p2]);
                    if (kk[p1] < kk[p2]) {
                        //printf("kk[p1]<kk[p2]\n");
                        *(*p + n + q) = kk[p1];
                        //printf("sort %d %d\n", q, *(*p + n + q));
                        p1++;
                        //printf("p1++=%d\n", p1);                      
                            break;                      
                    }
                    else {
                        //printf("kk[p1]>kk[p2]\n");
                        *(*p + n + q) = kk[p2];
                        //printf("sort %d %d\n",q,*(*p + n + q));
                        p2++;
                        //printf("p2++=%d\n", p2);                  
                        break;
                    }
                }
            }
        }           
            }
        }                               

void swap(int *a, int *b) {
    int num;
    num = *b;
    *b = *a;
    *a = num;
}

你可能感兴趣的:(冒泡,选择,归并排序)