zoukankan      html  css  js  c++  java
  • 6种基本排序(C++实现)

    六种基本排序...包括冒泡排序,直接插入排序,直接选择排序,希尔排序,归并排序,快速排序. 第七种堆排序目前理解的不是很好,未完成.

    运行时间方便比较排序时间效率,N可以改变数组元素个数方便测试,随机数用来生成随机数组.

    C++实现如下:

    // arraySort.cpp : 数组排序
    //
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    const int N = 100;		//数组元素个数
    const int RAND = 1000;	//随机数范围
    void sortMP(int a[], int n);	//冒泡排序
    void sortZC(int a[], int n);	//直接插入排序
    void sortZX(int a[], int n);	//直接选择排序
    void sortShell(int a[], int n);	//希尔排序
    void sortKP(int a[], int l, int r);	//快速排序
    void sortD(int a[], int n);		//堆排序(暂未完成)
    
    void sortG(int a[], int low, int mid, int high, int temp[]);//归并排序(合并)
    void sortMerge(int a[], int low, int high, int temp[]);//归并排序(递归) 主函数调用
    void printarray(int a[],int n);	//打印数组
    int main()
    {
    	int a[N];
    	srand((int)time(NULL));     //每次执行种子不同,生成不同的随机数
    	for (int i = 0; i < N; i++)
    	{
    		a[i] = rand() % RAND;
    	}
    	clock_t start_time = clock();
    
    	sortMP(a, N);
    	
    	//sortZC(a, N);
    
    	//sortZX(a, N);
    
    	//sortShell(a, N);
    	
    	//   归并排序
    	/*
    	int *p = new int[N];
    	sortMerge(a, 0,N-1,p);
    	cout<<"归并排序结果: ";
    	printarray(a, N);
    	*/
    
    	//	快速排序
    	/*	
    	sortKP(a,0,N-1);
    	cout << "快速排序结果: ";
    	printarray(a, N);
    	*/
    	//sortD(a, N); 暂未完成
    
    	clock_t end_time = clock();
    	cout << "运行时间: " << static_cast<double>(end_time - start_time) / CLOCKS_PER_SEC * 1000 << "ms" << endl;
    	system("pause");
        return 0;
    }
    
    void sortMP(int a[], int n)//冒泡排序
    {
    	for (int i = 0; i < n; i++)
    	{
    		for (int j = 1; j < n - i; j++)
    			if (a[j - 1] < a[j])	//从大到小
    				swap(a[j - 1], a[j]);
    	}
    	cout << "冒泡1排序结果: ";
    	printarray(a, n);
    	
    	int flag = n;
    	while (flag > 0)
    	{
    		int k = flag;
    		flag = 0;
    		for (int j = 1; j < k; j++)//从小到大
    		{
    			swap(a[j - 1], a[j]);
    			flag = j;
    		}
    	}
    	cout << "冒泡2排序结果: ";
    	printarray(a, n);
    }
    
    void sortZC(int a[], int n)//直接插入排序
    {
    	for (int i = 1; i < n; i++)
    	{
    		int j = 0;
    		if (a[i] < a[i - 1])
    		{
    			int temp = a[i];
    			for (j = i - 1; j >= 0 && a[j] > temp; j--)
    				a[j + 1] = a[j];
    			a[j + 1] = temp;
    		}
    	}
    	cout << "直接插入结果: ";
    	printarray(a, n);
    }
    
    void sortZX(int a[], int n)	//直接选择排序
    {
    	int min;
    	for (int i = 0; i < n; i++)
    	{
    		min = i;
    		for(int j=i+1;j<n;j++)
    			if (a[j] < a[min])
    			{
    				min = j;
    			}
    		swap(a[i], a[min]);
    	}
    	cout << "直接选择结果: ";
    	printarray(a, n);
    }
    
    void sortShell(int a[], int n)//希尔排序
    {
    	/****** 本块方便理解
    	int gap;
    	for (gap = n / 2; gap > 0; gap /= 2)
    		for (int i = 0; i < gap; i++)
    		{
    			for (int j = i + gap; j < n; j += gap)
    			{
    				if (a[j] < a[j - gap])
    				{
    					int temp = a[j];
    					int k = j - gap;
    					while (k >= 0 && a[k] > temp)
    					{
    						a[k + gap] = a[k];
    						k -= gap;
    					}
    					a[k + gap] = temp;
    				}
    			}
    		}*/
    	int gap;	//此块分排序部分为冒泡思想
    	for(gap =n/2;gap>0;gap/=2)
    		for(int i=gap;i<n;i++)
    			for (int j = i - gap; j >= 0 && a[j] > a[j + gap]; j -= gap)
    			{
    				swap(a[j], a[j + gap]);
    			}
    	cout << "希尔排序结果: ";
    	printarray(a, n);
    }
    
    void sortG(int a[], int low, int mid, int high, int temp[])//归并排序(合并)
    {
    	int i, j, k;
    	i = low;
    	j = mid + 1;//避免重复比较a[mid]
    	k = 0;
    	while (i <= mid && j <= high)
    	{
    		if (a[i] <= a[j])
    			temp[k++] = a[i++];
    		else
    			temp[k++] = a[j++];
    	}
    	while (i <= mid)
    		temp[k++] = a[i++];
    	while (j <= high)           //a[low,mid]已经全部归入temp数组,(mid,high]还有剩余
    		temp[k++] = a[j++];
    
    	for (i = 0; i < k; i++)
    		a[low + i] = temp[i];     //应从a[low+i]开始赋值
    }
    
    void sortMerge(int a[], int low, int high, int temp[])//归并排序(递归) 主函数调用
    {
    	if (low < high)
    	{
    		int mid = (low + high) / 2;
    		sortMerge(a, low, mid, temp);      //左边有序
    		sortMerge(a, mid + 1, high, temp);   //右边有序
    		sortG(a, low, mid, high, temp);     //合并
    	}
    }
    
    void sortKP(int a[], int l, int r)	//快速排序
    {
    	if (l < r)
    	{
    		int i = l, j = r, x = a[l];
    		while (i < j)
    		{
    			while (i < j&&a[j] >= x)
    				j--;
    			if (i < j)
    				a[i++] = a[j];
    
    			while (i < j&&a[i] < x)
    				i++;
    			if (i < j)
    				a[j--] = a[i];
    		}
    		a[i] = x;
    		sortKP(a, l, i - 1);	//左
    		sortKP(a, i + 1, r);	//右
    	}
    }
    
    void sortD(int a[], int n)		//堆排序(未完成)
    {
    
    }
    
    void printarray(int a[],int n)	//打印函数
    {
    	for (int i = 0; i < n; i++)
    		cout << a[i] << " ";
    	cout << endl;
    }


  • 相关阅读:
    Linux三剑客grep、awk和sed
    Appium元素定位(二):UiAutomator定位
    Appium元素定位(一)
    App控件定位
    Appium 介绍及环境安装
    android Mvp简单实用
    EventBus通信
    Activity关闭另一个Acitivity
    Android输入法 监听事件
    图片形状圆角
  • 原文地址:https://www.cnblogs.com/yunet/p/12584099.html
Copyright © 2011-2022 走看看