--c++ implementation of various sorting algorithms

Source: Internet
Author: User
Tags int size
#include <iostream> using namespace std;  Heap sort void findmaxinheap (int arr[], int size) {for (int j = size-1 J > 0;j-) {int parent = j/   
        2;   
        int child = J;   
        if (J < size-1 && Arr[j] < arr[j+1]) {++child;   
            } if (Arr[child] > Arr[parent]) {int tmp = Arr[child];   
            Arr[child] = arr[parent];   
        Arr[parent] = tmp; }} void Heapsort (int arr[], int size) {for (int j = size; j > 0; j--) {Findmaxinhe   
        AP (arr, J);   
        int tmp = arr[0];   
        Arr[0] = arr[j-1];  
		
    ARR[J-1] = tmp;
	}//Quick sort void quicksort (int a[],int left,int right) {int i,j,temp;
	I=left;
	J=right;
	Temp=a[left];
	if (left>right) return;
		while (I!=J) {while (a[j]>temp && j>i) j--;
		if (j>i) a[i++]=a[j];
		while (a[i]<temp && j>i) i++;
		if (j>i)	A[j--]=a[i];
	} a[i]=temp;
	Quicksort (a,left,i-1);
Quicksort (A,i+1,right);
	//select sort void selectsort (int a[],int len) {int i,j,min,temp;
		for (i=0;i<len;i++) {min=i;
			for (j=i+1;j<len;j++) {if (A[j]<a[min]) {min=j;
			} if (min!=i) {temp=a[i];
			A[i]=a[min];
		A[min]=temp;
	Direct insert sort void insertsort (int a[],int len) {int i,j,temp,min;
		for (j=1;j<len;j++) {min=a[j];
				for (i=j-1;i>=0;i--) {if (a[i]>min) {temp=a[i];
				A[I]=A[I+1];
				A[i+1]=temp;
			Min=a[i];
	}}//bubble sort void bubblesort (int a[],int len) {int i,j,temp,is_swp;
				for (i=0;i<len;i++) {for (j=0;j<len-i-1;j++) {if (a[j]>a[j+1]) {temp=a[j];
				A[J]=A[J+1];
			A[j+1]=temp;
}} return;   
    int main () {int arr[] = {13, 5, 3, 12, 6, 21, 8, 19};
	int n = sizeof (arr)/sizeof (arr[0));
    cout<< "Sort result:" <<endl;  
	Heapsort (arr, n);
	Quicksort (arr,0,n-1);
	Selectsort (Arr,n); InsErtsort (Arr,n);
	Bubblesort (Arr,n);
    for (int i=0;i<n;i++) cout<<arr[i]<< "";  
	cout<<endl;
return 0; }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.