zoukankan      html  css  js  c++  java
  • 排序,查找

    快速排序

    #include <stdio.h>
    
    int partition(int *a, int first, int last)
    {
    	int temp = a[first];
    	while (last > first){
    		while (last > first && a[last] >= temp) last --;
    		if (last > first){
    			a[first] = a[last];
    			first ++;
    		}						
    		while (last > first && a[first] <= temp) first ++;
    		if (last > first){
    			a[last] = a[first];
    			last --;
    		}
    	}
    	a[first] = temp;
    	return first;
    }
    
    void myqsort(int a[], int first, int last)
    {
    	int part;
    	if (first < last){
    		part = partition(a, first, last);
    		myqsort(a, first, part);
    		myqsort(a, part+1, last);
    	}
    	
    }
    
    void quicksort(int a[], int n)
    {
    	int first = 0;
    	int last = n-1;
    	myqsort(a, first, last);
    }
    
    int main()
    {
    	int a[] = {5, 2, 1, 9, 0, 8, 6, 4, 7, 3};
    	int n = sizeof(a)/sizeof(a[0]);
    	quicksort(a, n);
    	int i;
    	for (i = 0; i < n; i++)
    		printf("%d ", a[i]);
    		
    	return 0;
    	
    }
    
    

    冒泡排序

    #include <stdio.h>
    
    void bubblesort(int a[], int n)
    {
    	int i, j;
    	int temp;
    	int flag;
    	for (i = 1; i < n; i ++){
    		flag = 0;
    		for (j = n-1; j >= i; j --){
    			if (a[j] < a[j-1]){
    				temp = a[j];
    				a[j] = a[j-1];
    				a[j-1] = temp;
    				flag = 1;
    			}
    		}
    		if (flag == 0) break;
    	}
    }
    
    
    int main()
    {
    	int a[]	= {3, 2, 9, 5, 1, 0, 6};
    	int n = sizeof(a)/sizeof(a[0]);
    	bubblesort(a, n);
    	int i;
    	for (i = 0; i < n; i ++){
    		printf("%d ", a[i]);
    	} 
    	
    	return 0;
    }
    

    插入排序

    #include <stdio.h>
    void insertsort(int a[], int n)
    {
    	int i, j;
    	int temp;
    	for (i = 1; i < n; i ++){
    		for (j = i; j > 0; j --){
    			if (a[j] >= a[j-1]) break;
    			else
    			{
    				temp = a[j];
    				a[j] = a[j-1];
    				a[j-1] = temp;
    			}
    		}
    	}
    }
    
    int main()
    {
    	int a[] = {3, 2, 8, 7, 0, 1, 6};
    	int n = sizeof(a)/sizeof(a[0]);
    	
    	insertsort(a, n);
    	int i;
    	for (i = 0; i < n; ++i){
    		printf("%d ", a[i]);
    	}
    	return 0;
    }
    

    归并排序

    #include <iostream>
    using namespace std;
    
    void Merge(int a[], int p, int q, int r)
    {
    	int n1 = q - p + 1;
    	int n2 = r - q;
    	int *L = new int[n1 + 1];
    	int *R = new int[n2 + 1];
    	int i, j, k;
    	for (i = 0; i < n1; i++)
    		L[i] = a[p + i];
    	for (j = 0; j < n2; j++)
    		R[j] = a[q + j + 1];
    
    	L[n1] = 0xFFFF, R[n2] = 0xFFFF;
    
    	i = 0; j = 0;
    	for (k = p; k <= r; k++){
    		if (L[i] <= R[j])
    			a[k] = L[i++];
    		else
    			a[k] = R[j++];
    	}
    }
    
    void Merge_sort(int a[], int p, int r)
    {
    	if (p < r){
    		int q = (p + r) / 2;
    		Merge_sort(a, p, q);
    		Merge_sort(a, q + 1, r);
    		Merge(a, p, q, r);
    	}
    }
    
    int main()
    {
    	int a[] = { 4, 2, 1, 8, 5, 3, 9, 2, 0 };
    	int n = sizeof(a) / sizeof(a[0]);
    	Merge_sort(a, 0, n - 1);
    	for (int i = 0; i < n; i++)
    		cout << a[i] << " ";
    	cout << endl;
    	return 0;
    }
    

    堆排序

    #include <iostream>
    using namespace std;
    
    #define LeftChild(i) (2*(i) + 1)
    
    void Swap(int &a, int &b)
    {
    	int tmp;
    	tmp = a;
    	a = b;
    	b = tmp;
    }
    void PercDown(int a[], int i, int N)
    {
    	int child;
    	int tmp;
    	for (tmp = a[i]; LeftChild(i) < N; i = child){
    		child = LeftChild(i);
    		if (child != N - 1 && a[child + 1] > a[child])
    			child++;
    		if (tmp < a[child])
    			a[i] = a[child];
    		else
    			break;
    	}
    	a[i] = tmp;
    }
    
    void HeapSort(int a[], int N)
    {
    	int i;
    	for (i = N / 2; i >= 0; i--)
    		PercDown(a, i, N);
    	for (i = N - 1; i > 0; i--){
    		Swap(a[0], a[i]);
    		PercDown(a, 0, i);
    	}
    }
    
    int main()
    {
    	int a[] = { 1, 4, 2, 9, 7, 0, 9, 5, 6, 3 };
    	int n = sizeof(a) / sizeof(a[0]);
    
    	HeapSort(a, n);
    	for (int i = 0; i < n; i++)
    		cout << a[i] << " ";
    
    	cout << endl;
    	return 0;
    }
    

    二分查找

    #include <stdio.h>
    int mybsearch(int a[], int n, int target)
    {
    	int first = 0, last = n-1, mid;
    	while (first <= last){
    		mid = (first+last)/2;
    		if (a[mid] == target) return mid;
    		else if (a[mid] < target) first = mid+1;
    		else last = mid-1;
    	}
    	return 0;
    }
    
    int main()
    {
    	int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    	int n = sizeof(a)/sizeof(a[0]);
    	printf("%d ", mybsearch(a, n, 5));
    	return 0;
    }
    
  • 相关阅读:
    linux脚本Shell之awk详解(二)
    linux脚本Shell之awk详解
    漫谈计算摄像学 (二):利用光场实现“先拍照后对焦”
    漫谈计算摄像学 (一):直观理解光场(Light Field)
    利用OpenCV检测图像中的长方形画布或纸张并提取图像内容
    2048理论上最高能玩到多少分?
    蛋疼之作:99行代码的2048
    用一个玩具例子说明基于视频的超分辨率重建的基本思想
    [C++]二维数组还是一维数组?
    三维空间中如何寻找和一组给定直线垂直程度最高的直线
  • 原文地址:https://www.cnblogs.com/shamoof/p/4463233.html
Copyright © 2011-2022 走看看