Merge sort and quick sort comparison "algorithm design and Analysis Experiment Report"

Source: Internet
Author: User
Tags array length comparison current time rand

The following source code is modified by the time difference is accurate to the nanosecond level of the other, but still feel very error. No matter how it is measured, it is always quick to merge, even if the array length of the test data is within 10.

The previous program writes the time is accurate to the microsecond level, the array length is probably within 10,000, is merges the sort to be quick, is bigger than this length fast sorting relatively fast. Combined with the above situation, the array of hours, the difference between the two will not be too much, so the individual think it is a quick sort of better, alas still think the merger is relatively simple and good to write, weak explosion ah ...

#include <cstdio> #include <Windows.h> #include <ctime> #include <cstdlib> #include <cstring

> #include <iostream> using namespace std;     const int MAXN = 100000000;
/* MAXN Array Maximum */* *a[] for randomly generated arrays, b[] copy random array, t[] for merge sort, temporarily save a[] */int A[MAXN];
int B[MAXN], T[MAXN];                        int n;
	/* n is the number of numbers to compare */* * The number of the array subscript from P to r is sorted * so that the preceding number is not greater than the datum element, the number behind is not less than the datum element */int partition (int p, int r) {int i = P;
	int j = r+1;

	int x = a[p];
		while (true) {while (A[++i] < x && I < R);

		while (A[--j] > x);

		if (i >= j) break;
		Swap (A, I, j);
		int temp = A[i];
		A[i] = A[j];
	A[J] = temp;
	} A[p] = A[j];
	A[J] = x; Return J; /* Returns the position of the base element in the order of */} int randomizedpartition (int p, int r)/* Process the datum element */{int i = rand ()% (r-p+1) + p;/* generates P to Q    Random number I */int temp = A[i];
	/* Swap the random position to the start position to make it a datum element */a[i] = a[p];

	A[P] = temp;
return partition (P, R); } void Randomizedquicksort (int p, int r)/* Quick Sort */{if (P < r) {inT q = randomizedpartition (P, R);      /* Split */Randomizedquicksort (P, q-1);
	/* Sort by the sides separately */Randomizedquicksort (q+1, R);       }} void Merge_sort (int *a, int x, int y, int *t)/* Merge sort */{if (Y-x > 1) {int m = (y-x)/2 + x;

		/* Take intermediate */int p = x, q = m, i = x;   Merge_sort (b, p, M, T);

		/* On both sides of the order */Merge_sort (b, M, y, T);
			while (P < m && Q < y) {if (B[p] <= b[q]) t[i++] = b[p++];
		else t[i++] = b[q++];
		} while (P < m) t[i++] = b[p++];

		while (Q < y) t[i++] = b[q++];
	for (i = x; i < y; i++)/* Re-deposit the sorted part a[]*/b[i] = t[i];
    }} int main () {printf ("Please enter the number of comparisons to sort n (n <= 100000000):");
        while (scanf ("%d", &n)! = EOF) {Large_integer begin, end;
        Double Tstart, tend, mergesortcost, quicksortcost;

        Long long mergesortcost, quicksortcost;       memset (A, 0, sizeof (a));
     /* Empty array to 0 */for (int i = 0; i < n; i++)/* Generate random number */{A[i] = rand ();       B[i] = A[i]; } queryperformancecounter (&begin);
        Tstart = Clock (); Randomizedquicksort (0, n-1); /* Quick Sort */QueryPerformanceCounter (&end);

        tend = clock (); Quicksortcost = end. Quadpart-begin. QuadPart;
        Tend-tstart;


        for (int i = 0; i < n; i++) printf ("%d", a[i]); QueryPerformanceCounter (&begin);
        Tstart = Clock (); Merge_sort (b, 0, N, t); /* Merge Sort */QueryPerformanceCounter (&end);

        tend = clock (); Mergesortcost = end. Quadpart-begin. QuadPart;
        Tend-tstart;

        for (int i = 0; i < n; i++) printf ("%d", b[i]);


        printf ("Quick sort time:%lld ns, merge sort time:%lldns\n\n\n", quicksortcost*100, mergesortcost*100);

    printf ("If you want to continue the comparison, continue to enter the number of comparisons N:");
} return 0;
 }
Below the nanosecond level, the feeling is not very accurate diagram bar.

Using this algorithm to analyze the comparison of two sorting methods, I also learned some simple function of the use of the head.


First, about the generation of random numbers.

/
* * Generate random integer between interval [a, b]
*
/#include <cstdio>
#include <cstdlib>
int main ()
{
	int A, b;
	while (scanf ("%d%d", &a, &b)! = EOF)
	{
		int ans = rand ()% (b-a+1) + A;
		printf ("%d\n", ans);
	}
	return 0;
	
}



Second, how to test the current time of the system.

/* microsecond time Test *
/#include <time.h>  //time.h is a date and time header file in C/s + +
#include <cstdio>
#include <iostream>
using namespace std;
int main ()
{
    Double tstart, tend, tcost;

    Tstart = Clock ();
    for (int i=0;i<10000000;i++); Do something
        tend = clock ();
    Tcost = (double) (Tend-tstart)/clocks_per_sec;

    printf ("%lf\n", tcost);
    cout<<clocks_per_sec; = =
    0 ms return;
}


/* nanosecond time Test One, from Zhou Jie seniors
*
/#include <Windows.h>
#include <stdio.h>

int main ()
{
	large_integer li;
	QueryPerformanceCounter (&li);
	printf ("%lld\n", Li);
	QueryPerformanceCounter (&li);
	printf ("%lld\n", Li);
	return 0;
}


/* nanosecond time Test II: When testing, from Zhou Jie seniors
*
/#include <Windows.h>
#include <stdio.h>

int main ()
{
	large_integer begin, end;
	QueryPerformanceCounter (&begin);
	Sleep (0); Do something
	QueryPerformanceCounter (&end);
	Long Long Duration = end. Quadpart-begin. QuadPart;
	printf ("Duration:%lldns\n", duration *);
    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.