QuickSort (MFC)

Source: Internet
Author: User

#include "stdafx.h" #include <afxmt.h> #include <iostream> #include <afxwin.h> #include "time.h" # Define Num 10000000using namespace Std;long wht_length;long wht_array[num];long Wht_array1[num];long Wht_array2[num]; CEvent FaxEvent1 (FALSE); CEvent FaxEvent2 (FALSE); CEvent FaxEvent3 (FALSE); CEvent faxevent (FALSE);/* Function name: QuickSort * Function: Serial fast sequencing of array sequences with starting and ending positions of Wht_start and Wht_end. * Input: Unordered array wht_array[1,wht_n] * return: Ordered array wht_array[1,wht_n] */int QuickSort (long R[],long Wht_start, long wht_end) {long i=w Ht_start,j=wht_end;long wht_tmp;if (wht_start<wht_end) {wht_tmp=r[wht_start];while (i!=j) {while (j>i& &wht_array[j]>=wht_tmp) J--;wht_array[i]=wht_array[j];while (i<j&&r[i]<=wht_tmp) i++;wht_ Array[j]=wht_array[i];} wht_array[i]=wht_tmp; QuickSort (wht_array,wht_start,i-1); QuickSort (Wht_array,i+1,wht_end); }return 0;} /* Function Name: QUICKSORT1 * Function: Serial fast sequencing of array sequences with starting and ending positions of Wht_start and Wht_end. * Input: Unordered array WHT_ARRAY[1,WHT_N/2] * return: Ordered array WHT_ARRAY[1,WHT_N/2] */int QuickSort1 (longWht_array1[],long Wht_start, Long wht_end) {long I=wht_start,j=wht_end;long wht_tmp;if (wht_start<wht_end) {wht_tmp =wht_array1[wht_start];while (I!=J) {while (j>i&&wht_array1[j]>=wht_tmp) j--;wht_array1[i]=wht_ Array1[j];while (i<j&&wht_array1[i]<=wht_tmp) i++;wht_array1[j]=wht_array1[i];} wht_array1[i]=wht_tmp; QuickSort1 (wht_array1,wht_start,i-1); QuickSort1 (Wht_array1,i+1,wht_end); }return 0;} /** function Name: Merge * Function: Merges the two arrays that are already sorted to form an ordered array. * Input: Ordered array WHT_ARRAY2[1,WHT_N/2],WHT_ARRAY1[1,WHT_N/2] * return: Ordered array wht_array[1,wht_n] */void Merge (Long wht_array[],long Wht_start,long Wht_mid,long wht_length) {Long *wht_array_temp;long i=wht_start,j=wht_mid+1,wht_k=0;wht_array_temp= ( Long *) malloc ((wht_length-wht_start+1) *sizeof (long)), while (i<=wht_mid&&j<=wht_length) if (Wht_array [I]<=wht_array[j]) {wht_array_temp[wht_k]=wht_array[i];i++;wht_k++;} else{wht_array_temp[wht_k]=wht_array[j];j++;wht_k++;} while (I<=wht_mid) {WHT_ARRAY_TEMP[WHT_K]=WHT_ARRAY[I];I++;WHt_k++;} while (j<=wht_length) {wht_array_temp[wht_k]=wht_array[j];j++;wht_k++;} for (wht_k=0,i=wht_start;i<=wht_length;wht_k++,i++) Wht_array[i]=wht_array_temp[wht_k];free (wht_array_temp);} UINT threadProc4 (lpvoid param) {{QuickSort (wht_array,0,wht_length/2-1);} SetEvent (faxEvent1); return 0;} UINT threadProc5 (lpvoid param) {{QuickSort (wht_array,wht_length/2,wht_length-1);} SetEvent (faxEvent2); return 0;} UINT threadProc6 (lpvoid param) {{Merge (wht_array,0,wht_length/2-1, wht_length-1);} SetEvent (FAXEVENT3); return 0;} UINT threadProc7 (lpvoid param) {{QuickSort1 (wht_array1,0,wht_length-1);} SetEvent (faxevent); return 0;} int _tmain (int argc, _tchar* argv[]) {//cout<< "MFC implementation" <<endl;//cout<< "input array length: =";//cin>>wht_ Length;//while (TRUE) {printf ("Please enter the number of random numbers generated:"); cin>>wht_length;/* read out the correct cin>>wht_length; return; otherwise, continue to require input * if ((wht_length>=90000) && (wht_length<=10000000)) {//break;} printf ("Input error, number range is [90000~100000]");} cout<< "Random number Generation:" <<endl;srand (396); for (int i=0;i<wht_length;i++) {wht_array[i]=rand ();//Generate random number wht_array1[i]=wht_array[i];cout<<wht_array[i]< < "";}    cout<<endl;clock_t T1=clock (); AfxBeginThread (Threadproc7,null); WaitForSingleObject (faxevent,infinite); clock_t t2=clock ();cout<< "serial result" <<endl;for (int i=0;i<wht_ length;i++) cout<<wht_array[i]<< ""; cout<<endl;double cost1=t2-t1;cout<< "Serial Time: =" << cost1<<endl;clock_t T3=clock (); AfxBeginThread (Threadproc4,null); AfxBeginThread (Threadproc5,null); WaitForSingleObject (Faxevent1,infinite); WaitForSingleObject (Faxevent2,infinite); AfxBeginThread (Threadproc6,null); WaitForSingleObject (faxevent3,infinite); clock_t t4=clock ();cout<< "parallel Result:" <<endl;for (int i=0;i<wht_ length;i++) cout<<wht_array[i]<< ""; cout<<endl;double cost2=t4-t3;cout<< "Parallel Time: =" << cost2<<endl;cout<< "acceleration ratio:" <<cost1/cost2<<endl;system ("pause"); return 0;}

QuickSort (MFC)

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.