Data structure job-L2

Source: Internet
Author: User

Question:Compile"Simple selection and sortingAndHeap sorting"Algorithm to compare the number of keyword comparisons and the number of moving keywords in the sorting process.

Test data:

(1) generate 100 random test data;

(2) write the test results.

Insert elements and adjust the heap up:

 

 

After deleting the element, adjust the heap downward:

 

 

4. debugging Analysis

This program implements a total of five sorting methods, you can choose to execute.

The number of comparisons and exchanges of keywords in various sorting algorithms is related to the number of data to be sorted, if there are accurate numbers after each exchange and comparison (of course I have tried), it is not enough for the overall analysis. When sorting a large amount of data, we consider the overall performance, rather than accurately comparing how many times and how many times of exchange, so I drew a chart of the sorting algorithm, which is more intuitive for comparing and exchanging keyword analysis, and can also be analyzed as a whole.

The data in this program is already ordered after a sorting. At this time, if we proceed to the next sorting, it is very likely that the performance of the algorithm will not be very good. Therefore, we recommend that you restart the program after executing a sorting operation, so that random data is generated and sorted again.

 

5. Instructions for use

Run the program and follow the prompts.

6. Test Results

1. When 10000 data records are randomly generated, the execution time of each algorithm is:

Insert and sort directly, time consuming: 141

Heap sorting, time consumption 0

Bubble sorting, time consumed: 500

Directly select sorting, 219 elapsed time

Fast sorting, time consumption 0

2. When 100000 data records are randomly generated, the execution time of each algorithm is:

Insert and sort directly, time consuming: 15109

Heap sorting, time consumption 47

Bubble sorting, time consumed: 50000

Directly select sorting, 23297 elapsed time

Fast sorting, time consumption 16

It can be seen that the fast sorting is indeed fast, and the heap sorting is not inferior. With the increase in the number of data, the time consumption of bubble, direct insertion, and direct selection is also growing rapidly.

 

7. Appendix

Source program file list.

Main. CPP # include "stdafx. H "# include <iostream> # include <stdlib. h> # include <time. h> # include <math. h> # include "minheap. H "using namespace STD; # define datatype int # define n 100000 datatype A [n]; // output array element void print () {for (INT I = 0; I <n; I ++) {cout <"A [" <I <"] =" <A [I] <"";}} // Insert the sort void insertsort (datatype array [], int N) {cout <"insert sort directly" <Endl; int I, j; datatype temp; for (I = 0; I <n-1; I ++) {temp = array [I + 1]; j = I; whil E (j> = 0 & temp <array [J]) {array [J + 1] = array [J]; j --;} array [J + 1] = temp ;}// directly select the sort, also called simple select sort void selectsort (datatype array [], int N) {cout <"select sort directly" <Endl; int I, j, small; datatype temp; for (I = 0; I <n; I ++) {small = I; for (j = I + 1; j <n; j ++) {If (array [small]> array [J]) {small = J ;}} if (small! = I) {temp = array [I]; array [I] = array [small]; array [small] = temp ;}}// heap sorting, small top heap void heapsort (datatype array [], int N) {cout <"heap sorting" <Endl; minheap H (array, N ); // heap int I, j; datatype temp; for (I = n-1; I> 0; I --) {temp = H. delete (); array [I] = temp;} // reverse, in the ascending order of for (j = 0; j <n/2; j ++) {temp = array [J]; array [J] = array [n-1-j]; array [n-1-j] = temp;} // bubble sort void bubblesoort (datatype array [], int N) {cout <"bubble sort" <Endl; int I, j, flag = 1; datatyp E temp; for (I = 1; I <n & flag = 1; I ++) {flag = 0; For (j = 0; j <n-I; j ++) {If (array [J]> array [J + 1]) {flag = 1; temp = array [J]; array [J] = array [J + 1]; array [J + 1] = temp ;}}// fast sort void quicksort (datatype array [], int low, int high) {int I = low; Int J = high; datatype temp = array [low]; while (I <j) {// from right to left while (I <J & temp <array [J]) {J --;} if (I <j) {array [I] = array [J]; I ++;} // from left to right while (I <J & array [I] <temp) {I ++;} if (I <j) {array [J] = array [I]; J -- ;}} array [I] = temp; If (low <I) {quicksort (array, low, I-1);} if (I <pigh) {quicksort (array, J + 1, high) ;}} int _ tmain (INT argc, _ tchar * argv []) {// cout <n <"data records for testing, followed by:" <Endl; // generate data srand (INT) Time (null )); for (INT I = 0; I <n; I ++) {A [I] = rand (); // cout <"A [" <I <"] =" <A [I] <"";} /* printf ("/n % d insert sort/N", 1); printf ("% d Heap Sort/N", 2 ); printf ("% d bubble sort/N", 3); printf ("% d select sort/N", 4); printf ("% d fast sort/N ", 5); printf ("/n illegal input, Program Will exit/N "); printf ("/n Select Operation/N "); int N; while (1) {CIN> N; Switch (N) {Case 1: insertsort (A, n); cout <"Result:"; print (); cout <Endl; break; Case 2: heapsort (, n); cout <"Result:"; print (); cout <Endl; break; Case 3: bubblesoort (A, n); cout <"result: "; print (); cout <Endl; break; Case 4: selectsort (A, n); cout <" Result: "; print (); cout <Endl; break; Case 5: quicksort (A, 0, N-1); cout <"Result:"; print (); cout <Endl; break; default: exit (1) ;}} */clock_t St Art, finish; Start = clock (); // insertsort (A, n); // heapsort (A, n); // bubblesoort (A, N ); // selectsort (A, n); quicksort (A, 0, N-1); finish = clock (); cout <"Time consumed" <double (finish-start) <Endl; System ("pause"); Return 0;} minheap. h // small top heap file # pragma once # include <iostream> # include <stdlib. h> # include <stdio. h> using namespace STD; # define datatype intclass minheap {PRIVATE: datatype * heaparray; // array where data elements are stored int markarray; // mark int Maxheapsize; // Max number of elements int heaapsize; // number of current elements void filterup (int I); // void filterdown (int I) after inserting an element ); // After the element is deleted, the adjustment process is public: int count_compare; // comparison count int count_move; // movement count public: minheap (INT maxsize); minheap (datatype arr [], int N); // copy the constructor and change n elements to heap void insert (datatype & item); // Insert the element datatype Delete (); // Delete the heap top, that is, the smallest element datatype getheaptop () // return the heaptop element {return heaparray [0];} int heapsize () // returns the number of elements in the current heap {return heaap Size;} int heapempty () // whether the heap is empty {return heaapsize = 0;} int heapfull () // whether the heap is full {return heaapsize = maxheapsize ;} void print (); // output the elements in the current heap ~ When minheap (void) // The heap is not marked, delete the heap {If (markarray = 0) {Delete heaparray;} // cout <Endl <"heap sorting. "<" Comparison times "<count_compare <", number of moves "<count_move <Endl ;}; minheap. CPP // small top heap source file # include "stdafx. H "# include" minheap. H "// heap, adjusted to heap minheap: minheap (INT maxsize) {If (maxsize <= 0) {// cout <" the parameter is invalid "; exit (1) ;}maxheapsize = maxsize; heaapsize = 0; heaparray = new datatype [maxsize]; markarray = 0; count_compare = 0; count_move = 0 ;} // make the array into a heap and change it to the heap minheap: minheap (INT arr [], int N) {If (n <= 0) {// cout <"the parameter is invalid"; exit (1) ;}this-> count_compar E = 0; this-& gt; count_move = 0; maxheapsize = N; heaapsize = N; heaparray = arr; int currentpos = (n-1)/2; while (currentpos> = 0) {filterdown (currentpos); currentpos --; count_compare ++; // compares} count_compare ++; // compares markarray = 1;} // after inserting an element, adjust heap void minheap: filterup (int I) {int currentpos, parentpos; datatype target; currentpos = I; target = heaparray [I]; parentpos = (I-1)/2; while (currentpos! = 0) {If (heaparray [parentpos] <= target) {break;} else {heaparray [currentpos] = heaparray [parentpos]; currentpos = parentpos; parentpos = (currentPos-1) /2 ;}} heaparray [currentpos] = target; // Insert new element} // Insert new element void minheap: insert (datatype & item) {If (heaapsize = maxheapsize) {// cout <"heaapsize"; exit (1) ;}heaparray [heaapsize] = item; filterup (heaapsize); heaapsize ++ ;} // Delete the heap top element and adjust void minheap: filterdown (int I) {int currenrpos, childpos; datatype target; currenrpos = I; target = heaparray [I]; childpos = 2 * I + 1; while (childpos <peaapsize) {count_compare ++; // compare if (childpos + 1 <peaapsize) & (heaparray [childpos + 1] <= heaparray [childpos]) {childpos = childpos + 1; count_compare + = 2; // compare} If (target <= heaparray [childpos]) {count_compare ++; // compare break;} else {heaparray [currenrpos] = heaparray [childpos]; currenrpos = childpos; childpos = 2 * currenrpos + 1; count_move ++; // exchanged} count_compare ++; // compared} count_compare ++; // compare heaparray [currenrpos] = target;} // Delete the heap top, that is, the minimum element datatype minheap: delete () {If (heaapsize = 0) {// cout <"empty"; exit (1);} datatype item = heaparray [0]; heaparray [0] = heaparray [heaapSize-1]; heaapsize --; count_move ++; // exchanged filterdown (0); Return item;} // output element void minheap: Print () {for (INT I = 0; I <this-> heaapsize; I ++) {printf ("% d", this-> heaparray [I]) ;}}

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.