몇 가지 일반적인 정렬 방법 (C 언어 구현)

18865 단어 C 언어
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

//      
void InsertSort(int arry[], int n)
{
    int i, j;
    int temp;//    
    for (i = 1; i < n; i++)
    {
        temp = arry[i];
        for (j = i - 1; j >= 0; j--)
        {
            if (temp > arry[j])
                break;
            else
                arry[j + 1] = arry[j];
        }
        arry[j+1] = temp;
    }
}

//      
void SelectSort(int arry[], int n)
{
    int i, j;
    int temp;
    for (i = 0; i < n-1; i++)
    {
        temp = i;
        for (j = i + 1; j < n; j++)
        {
            if (arry[j] < arry[temp])
                temp = j;
        }
        if(temp!=i)
            arry[temp] ^= arry[i] ^= arry[temp] ^= arry[i];//  arry[temp] arry[i]   
    }
}

//    
void BubbleSort(int arry[], int n)
{
    int i, j, k;
    for (i = 0; i < n-1; i++)
    {
        for (j = i; j < n; j++)
        {
            if (arry[j - 1]>arry[j])
                arry[j - 1] ^= arry[j] ^= arry[j - 1] ^= arry[j];//  arry[temp] arry[i]   
        }
    }
}

//
void ShellSort1(int arry[], int n)
{
    int i, j, k, gap,temp;
    for (gap = n / 2; gap > 0; gap /= 2)
    {
        for (k = 0; k < gap; k++)//    
        {
            for (i = k+gap; i < n; i += gap)
            {
                temp = arry[i];
                for (j = i - gap; j >= 0; j -= gap)
                {
                    if (temp > arry[j])
                        break;
                    else
                        arry[j + gap] = arry[j];
                }
                arry[j + gap] = temp;
            }
        }
    }
}

//       
void ShellSort2(int arry[], int n)
{
    int i, j, k, gap, temp;
    for (gap = n / 2; gap > 0; gap /= 2)
    {
        for (i = gap; i < n; i++)//       
            {
                temp = arry[i];
                for (j = i - gap; j >= 0; j -= gap)
                {
                    if (temp > arry[j])
                        break;
                    else
                        arry[j + gap] = arry[j];
                }
                arry[j + gap] = temp;
            }        
    }
}

//    ,  gap           
void ShellSort3(int arry[], int n)
{
    int i, j, k, gap, temp;
    for (gap = n / 2; gap > 0; gap /= 2)
    {
        for (k = 0; k < gap; k++)
        {
            for (i = k; i < n-gap; i += gap)
            {
                for (j = k+gap; j <n ; j += gap)
                {
                    if (arry[j - gap]>arry[j])
                        arry[j - gap] ^= arry[j] ^= arry[j - gap] ^= arry[j];
                }
                
            }
        }
    }
}

//
void QuickSort(int arry[], int s,int t)
{
    int i, j;

    if (s < t)
    {
        i = s;
        j = t+1;
        while (1)
        {
            do
            {
                i++;
            }while (!(arry[s] <= arry[i] || i >= t));
            do
            {
                j--;
            } while (!(arry[s] >= arry[j] || j <= s));
            
            if (i < j)
                arry[i] ^= arry[j] ^= arry[i] ^= arry[j];
                
            else
                break;
        }
        if (arry[j]!= arry[s])
            arry[j] ^= arry[s] ^= arry[j] ^= arry[s];
        QuickSort(arry, s, j-1);
        QuickSort(arry, j + 1, t);
    }
}

int main()
{
    int arry1[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41};
    int arry2[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };
    int arry3[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };
    int arry4[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };
    int arry5[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };
    int arry6[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };
    int arry7[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };
    
    ////     :    >    >    ==  
    ////InsertSort(arry1, 15);
    
    //SelectSort(arry2, 15);
    
    //BubbleSort(arry3, 15);
    
    //ShellSort1(arry4, 15);
    //ShellSort2(arry5, 15);    
    //ShellSort2(arry6, 15);
    
    //QuickSort(arry7,0,14);
    


    int i = 0;
    for (i = 0; i < 15; i++)
    {
        printf("%d  ", arry7[i]);
    }

    system("pause");
}

좋은 웹페이지 즐겨찾기