Data structures and algorithms (17): Sort

 

Bubble Sort:


#include <stdio.h>

void BubbleSort1(int k[] , int n)
{
    int i , j , temp ,count1 = 0 , count2 = 0,num ;

    for(i = 0 ; i < n-1 ; i++)
    {
        for(j = i+1 ; j < n ; j ++)
        {
            count1++;
            if(k[i] > k[j])
            {
                count2++;
                temp = k[j];
                k[j] = k[i];
                k[i] = temp;

               for( num = 0; num < n; num++ )
	           {
                    printf("%d", k[num]);
               }
                printf("\n");
            }
        }
    }
    printf("总共进行了%d次的比较,进行了%d次的移动" ,count1,count2);
}

void BubbleSort2(int k[] , int n)
{
    int i , j ,temp ,count1 = 0 , num, count2 = 0;
    for(i = 0; i < n-1; i++)
    {
        for(j = n -1; j > i ; j--)
        {
            count1++;
            if( k[j-1] > k[j] )
            {
                count2++;
                temp = k[j -1];
                k[j-1] = k[j];
                k[j] = temp;
                for( num = 0; num < n; num++ )
	           {
                    printf("%d", k[num]);
               }
                printf("\n");

            }



        }

    }
   printf("总共进行了%d次比较,进行了%d次移动!", count1, count2);
}

void BubbleSort3(int k[] ,int n)   //有错误
{
    int i,j,num,temp,count1,count2,flag;

    flag = 1;
    for( i = 0; i < n-1 && flag ; i++)
    {
        for(j = n-1; j>= i;j--)
        {
           count1++;
           flag = 0;
           if(k[j - 1] > k[j])
           {
             count2++;
             temp = k[j-1];
             k[j-1] = k[j];
             k[j] = temp;
             flag = 1;
            for( num = 0; num < n; num++ )
            {
               printf("%d", k[num]);
            }
            printf("\n");
           }
        }
    }
    printf("总共进行了%d次比较,进行了%d次移动!", count1, count2);
}

int main()
{
	int i, a1[10] = {1, 0, 2, 8, 5, 4, 6, 7, 3, 9};
    int  a2[10] ={1, 0, 2, 8, 5, 4, 6, 7, 3, 9};
    int  a3[10] ={1, 0, 2, 8, 4, 5, 6, 7, 3, 9};

	BubbleSort1(a1, 10);

    printf("排序后的结果是:");
	for( i=0; i < 10; i++ )
	{
		printf("%d", a1[i]);
	}
	printf("\n\n");

	BubbleSort2(a2, 10);

    printf("排序后的结果是:");
	for( i=0; i < 10; i++ )
	{
		printf("%d", a2[i]);
	}
	printf("\n\n");

	BubbleSort3(a3, 10);

	printf("排序后的结果是:");
	for( i=0; i < 10; i++ )
	{
		printf("%d", a3[i]);
	}
	printf("\n\n");

	return 0;
}

Selection Sort

#include <stdio.h>

void SelectSort(int k[], int n)
{
	int i, j, min, temp, count1=0, count2=0;

	for( i=0; i < n-1; i++ )
	{
		min = i;
		
		for( j=i+1; j < n; j++ )
		{
			count1++;
			if( k[j] < k[min] )
			{
				min = j;
			}
		}

		if( min != i )
		{
			count2++;
			temp = k[min];
			k[min] = k[i];
			k[i] = temp;
		}
	}

	printf("总共进行了%d次比较,进行了%d次移动!", count1, count2);
}

int main()
{
	int i, a[10] = {5, 2, 6, 0, 3, 9, 1, 7, 4, 8};

	SelectSort(a, 10);

	printf("排序后的结果是:");
	for( i=0; i < 10; i++ )
	{
		printf("%d", a[i]);
	}
	printf("\n\n");

	return 0;
}

Direct insertion sort

#include <stdio.h>

void InsertSort(int k[], int n)
{
	int i, j, temp;

	for( i=1; i < n; i++ )
	{
		if( k[i] < k[i-1] )
		{
			temp = k[i];

			for( j=i-1; k[j] > temp; j-- )
			{
				k[j+1] = k[j];
			}

			k[j+1] = temp;
		}
	}
}

int main()
{
	int i, a[10] = {5, 2, 6, 0, 3, 9, 1, 7, 4, 8};

	InsertSort(a, 10);

	printf("排序后的结果是:");
	for( i=0; i < 10; i++ )
	{
		printf("%d", a[i]);
	}
	printf("\n\n");

	return 0;
}

Shell sort

#include <stdio.h>

void InsertSort(int k[], int n)
{
	int i, j, temp;
	int gap = n;

	do
	{
		gap = gap/3 + 1;

		for( i=gap; i < n; i++ )
		{
			if( k[i] < k[i-gap] )
			{
				temp = k[i];

				for( j=i-gap; k[j] > temp; j-=gap )
				{
					k[j+gap] = k[j];
				}

				k[j+gap] = temp;
			}
		}
	}while(gap > 1);
}

int main()
{
	int i, a[10] = {5, 2, 6, 0, 3, 9, 1, 7, 4, 8};

	InsertSort(a, 10);

	printf("排序后的结果是:");
	for( i=0; i < 10; i++ )
	{
		printf("%d", a[i]);
	}
	printf("\n\n");

	return 0;
}

Heapsort

#include <stdio.h>

int count = 0;

void swap(int k[], int i, int j)
{
	int temp;

	temp = k[i];
	k[i] = k[j];
	k[j] = temp;
}

void HeapAdjust(int k[], int s, int n)
{
	int i, temp;

	temp = k[s];

	for( i=2*s; i <= n; i*=2 )
	{
		count++;
		if( i < n && k[i] < k[i+1] )
		{
			i++;
		}

		if( temp >= k[i] )
		{
			break;
		}

		k[s] = k[i];
		s = i;
	}

	k[s] = temp;
}

void HeapSort(int k[], int n)
{
	int i;

	for( i=n/2; i > 0; i-- )
	{
		HeapAdjust(k, i, n);
	}

	for( i=n; i > 1; i-- )
	{
		swap(k, 1, i);
		HeapAdjust(k, 1, i-1);
	}
}

int main()
{
	int i, a[10] = {-1, 5, 2, 6, 0, 3, 9, 1, 7, 4};

	HeapSort(a, 9);

	printf("总共执行 %d 次比较!", count);
	printf("排序后的结果是:");
	for( i=1; i < 10; i++ )
	{
		printf("%d", a[i]);
	}
	printf("\n\n");

	return 0;
}

Merge sort

#include <stdio.h>
#define MAXSIZE 10

// 实现归并,并把最后的结果存放到list1里
void merging(int *list1, int list1_size, int *list2, int list2_size)
{
	int i, j, k, m;
	int temp[MAXSIZE];

	i = j = k = 0;

	while( i < list1_size && j < list2_size )
	{
		if( list1[i] < list2[j] )
		{
			temp[k++] = list1[i++];
		}
		else
		{
			temp[k++] = list2[j++];
		}
	}

	while( i < list1_size )
	{
		temp[k++] = list1[i++];
	}

	while( j < list2_size )
	{
		temp[k++] = list2[j++];
	}

	for( m=0; m < (list1_size + list2_size); m++ )
	{
		list1[m] = temp[m];
	}
}

void MergeSort(int k[], int n)
{
	if( n > 1)
	{
		int *list1 = k;
		int list1_size = n/2;
		int *list2 = k + n/2;
		int list2_size = n - list1_size;

		MergeSort(list1, list1_size);
		MergeSort(list2, list2_size);

		merging(list1, list1_size, list2, list2_size);
	}
}

int main()
{
	int i, a[10] = {5, 2, 6, 0, 3, 9, 1, 7, 4, 8};

	MergeSort(a, 10);

	printf("排序后的结果是:");
	for( i=0; i < 10; i++ )
	{
		printf("%d", a[i]);
	}
	printf("\n\n");

	return 0;
}

Inverse iteration

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10

void MergeSort(int k[], int n)
{
	int i, next, left_min, left_max, right_min, right_max;
	int *temp = (int *)malloc(n * sizeof(int));

	for( i=1; i < n; i*=2 ) 
	{
		for( left_min=0; left_min < n-i; left_min = right_max )
		{
			right_min = left_max = left_min + i;
			right_max = left_max + i;

			if( right_max > n )
			{
				right_max = n;
			}

			next = 0;

			while( left_min < left_max && right_min < right_max )
			{
				if( k[left_min] < k[right_min] )
				{
					temp[next++] = k[left_min++];
				}
				else
				{
					temp[next++] = k[right_min++];
				}
			}

			while( left_min < left_max )
			{
				k[--right_min] = k[--left_max];
			}

			while( next > 0 )
			{
				k[--right_min] = temp[--next];
			}
		}
	}
}

int main()
{
	int i, a[10] = {5, 2, 6, 0, 3, 9, 1, 7, 4, 8};

	MergeSort(a, 10);

	printf("排序后的结果是:");
	for( i=0; i < 10; i++ )
	{
		printf("%d", a[i]);
	}
	printf("\n\n");

	return 0;
}

Quick Sort

#include <stdio.h>

void swap(int k[], int low, int high)
{
	int temp;

	temp = k[low];
	k[low] = k[high];
	k[high] = temp;
}

int Partition(int k[], int low, int high)
{
	int point;

	point = k[low];

	while( low < high )
	{
		while( low < high && k[high] >= point )
		{
			high--;
		}
		swap(k, low, high);
		
		while( low < high && k[low] <= point )
		{
			low++;
		}
		swap(k, low, high);
	}

	return low;
}

void QSort(int k[], int low, int high)
{
	int point;

	if( low < high )
	{
		point = Partition(k, low, high);

		QSort(k, low, point-1);

		QSort(k, point+1, high);
	}
}

void QuickSort(int k[], int n)
{
	QSort(k, 0, n-1);
}

int main()
{
	int i, a[10] = {4, 2, 5, 0, 3, 9, 1, 7, 6, 8};

	QuickSort(a, 10);

	printf("排序后的结果是:");
	for( i=0; i < 10; i++ )
	{
		printf("%d", a[i]);
	}
	printf("\n\n");

	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_41543888/article/details/96633829