void HeapSelectSort(SqList &R,int flag) {//初始条件:顺序表R存在,0号位置未占用,flag为记录有序类型标记(0正序,1逆序) //操作结果:堆排序使R中记录按关键字有序 if(!flag) HeapSort(R,compare_ps); else HeapSort(R,compare_ns); }
/** * Function : Pat_hThread * Description : create a patient thread * Input : * Output : nothing * return : nothing */ void Pat_hThread(void *ptr) { int pre_min,pre_sec,i; int sign = 1 ;//上班时间控制 time_t nowtime,pretime; struct tm *pre_timeinfo,*now_timeinfo; time ( &pretime ); pre_timeinfo = localtime ( &pretime ); pre_min = pre_timeinfo -> tm_min; pre_sec = pre_timeinfo -> tm_sec; if(Init_PriorityQue(&Prio_Que)) { while(sign) { time ( &nowtime ); now_timeinfo = localtime ( &nowtime );//记录当前系统时间 if((now_timeinfo -> tm_hour >= MORWORK_BIGEN && now_timeinfo -> tm_hour < MORWORK_FINISH) || (now_timeinfo -> tm_hour >= AFTWORK_BIGEN && now_timeinfo -> tm_hour < AFTWORK_FINISH)) { // 当前时间为医生上班时间 // 病人开始挂号 if(abs(now_timeinfo -> tm_min - pre_min) >= 0 && abs(now_timeinfo -> tm_sec - pre_sec) == FIXED_TIME) { // 每超过5分钟产生下一个病人 while(!Priority_Insert(&Prio_Que)) { //挂号病人到达上限时,挂号停止,等待病人出列 Pat_LeaveJudge(&Prio_Que); //病人离开判断 QueueAdjust(&Prio_Que); //调整队列 HeapSort(&Prio_Que); //优先级重排 Sleep(1000*10); //等待10秒钟 } pretime = nowtime; pre_sec = now_timeinfo -> tm_sec; pre_min = now_timeinfo -> tm_min; Sleep(1000); //停顿1秒 } if(abs(now_timeinfo -> tm_min - pre_min) >= UPDATE_INTERVAL && abs(now_timeinfo -> tm_sec - pre_sec) == FIXED_TIME) { //每3分钟更新优先级 QueueAdjust(&Prio_Que); if(Prio_Que.length) { for(i = 1 ; i <= Prio_Que.length ; i ++) { //优先级更新 (Prio_Que.base)[i].key = Cal_Priority((Prio_Que.base)[i]); } HeapSort(&Prio_Que); //优先级重排 } } } else sign = 0 ; }//while }//if Destroy_PriorityQue(&Prio_Que) ; //就诊结束 }
int main(){ int array[ARRAY_SIZE]={}; int i=0; float seconds; clock_t end ,start; InitializeArray(array); printf("Unsorted array\n\n"); DisplayArray(array); HEAP heap = heapify(array,ARRAY_SIZE); //for(i=0;i<ARRAY_SIZE;i++){printf("%d at %d \t",heap->hArray[i],i+1); } start = clock(); HeapSort(heap,array); end = clock(); seconds = (float)(end - start) / CLOCKS_PER_SEC; //printf("\n\n"); printf("\nSorted array\n\n"); DisplayArray(array); printf("\n\nTime taken for sort= %f sec\n",seconds); return 0; }
int main() { int heap[] = {0, 49, 38, 65, 97, 76, 13, 27, 49}; /* heap[0] is neglect */ int hpLength = sizeof(heap) / sizeof(heap[0]); HeapSort(heap, hpLength - 1); ArrRpout(heap, hpLength - 1); }
InitTypeOfFE_PkEdge(int KK) :k(KK),npe(k+1),ndf(3*npe),X(npe),Data( 5*ndf+3) { //Pi_h_coef=1.; const QuadratureFormular1d QF(-1+2*npe,npe,GaussLegendre(npe),true); for (int i=0;i<npe;++i) X[i]=QF[i].x; HeapSort((R *) X,npe); int j=0; int o[6]; o[0]=0; for(int i=1;i<6;++i) o[i]=o[i-1]+ndf; for(int df=0;df<ndf;++df) { int e= df/npe; int n= df%npe; Data[o[0]+df]=3+e; Data[o[1]+df]=n; Data[o[2]+df]=e; Data[o[3]+df]=0; Data[o[4]+df]=df; } Data[o[5]] =0; Data[o[5]+1] =0 ; Data[o[5]+2] =ndf;// end_dfcomp }
int main() { TList l; InitList(&l, a); PrintList(&l, "original list: "); HeapSort(&l); PrintList(&l, "heap sorted list: "); }
int main() { int array[] = {1,3,7,9,14,8,4,2,10,16}; HeapSort(array, 10); for(int i=0; i<10; i++) printf("%d ", array[i]); printf("\n"); return 0; }
void TestHeapSort() { int arr[] = { 2, 5, 4, 9, 3, 6, 8, 7, 1, 0 }; //int arr[] = { 3, 2, 4, 6, 7, 13, 23, 67, 34, 25, 16 }; int len = sizeof(arr) / sizeof(arr[0]); HeapSort(arr, len); PrintArrray(arr, len); }
int main(int argc,char** argv) { printf("Before:\n"); pri(arr); HeapSort(arr,len); printf("After:\n"); pri(arr); return 0; }
int main(void) { short arr[] = { 1000, -22, 255, -1, 2, 7, 33, 10, 177, }; HeapSort(std::begin(arr), std::end(arr)); copy(std::begin(arr), std::end(arr), std::ostream_iterator<short>(std::cout, " ")); std::cout << std::endl; return 0; }
int main(int argc, char *argv[]) { /* 将数组看成完全二叉树的中序遍历结果的线性存储 */ int data[13]={8,5,4,6,13,7,2,9,12,11,3,10,1}; HeapSort(data,13); system("PAUSE"); return 0; }
void PQueueSortTest(void) { int i; pqueueADT pq; int array[SORT_SIZE]; printf("\n----------- Testing use of pqueue to sort -----------\n"); pq = NewPQueue(); printf("Enqueuing %d numbers into pqueue in increasing order.", SORT_SIZE); for (i = 0; i < SORT_SIZE; i++) array[i] = i; HeapSort(pq, array, SORT_SIZE); printf("\nUsing dequeue to pull out numbers in sorted order. Are they sorted? %s\n", ArrayIsSorted(array, SORT_SIZE) ? "TRUE" : "FALSE"); printf("PQueue should be empty. Is it empty? %s\n", IsEmpty(pq) ? "TRUE" : "FALSE"); printf("\nEnqueuing %d numbers into pqueue in decreasing order.", SORT_SIZE); for (i = 0; i < SORT_SIZE; i++) array[i] = SORT_SIZE - i; HeapSort(pq, array, SORT_SIZE); printf("\nUsing dequeue to pull out numbers in sorted order. Are they sorted? %s\n", ArrayIsSorted(array, SORT_SIZE) ? "TRUE" : "FALSE"); printf("PQueue should be empty. Is it empty? %s\n", IsEmpty(pq) ? "TRUE" : "FALSE"); printf("\nEnqueuing %d random values into the pqueue.\n", SORT_SIZE); for (i = 0; i < SORT_SIZE; i++) array[i] = RandomInteger(1, 1000); HeapSort(pq, array, SORT_SIZE); printf("Using dequeue to pull out numbers in sorted order. Are they sorted? %s\n", ArrayIsSorted(array, SORT_SIZE) ? "TRUE" : "FALSE"); printf("PQueue should be empty. Is it empty? %s\n", IsEmpty(pq) ? "TRUE" : "FALSE"); printf("\nEnqueuing %d random possibly negative values into the pqueue.\n", SORT_SIZE); for (i = 0; i < SORT_SIZE; i++) array[i] = RandomInteger(-1000, 1000); HeapSort(pq, array, SORT_SIZE); printf("Using dequeue to pull out numbers in sorted order. Are they sorted? %s\n", ArrayIsSorted(array, SORT_SIZE) ? "TRUE" : "FALSE"); printf("PQueue should be empty. Is it empty? %s\n", IsEmpty(pq) ? "TRUE" : "FALSE"); FreePQueue(pq); printf("Hit return to continue: "); { string s = GetLine(); FreeBlock(s); } }
int main() { int n,m,i; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%d",&a[i]); HeapSort(a,n); printf("%d",a[m]); system("pause"); return 0; }
// ----------------------------------------------------------------------------- bool Algorithms::Test_HeapSort() { bool pass = true; { int a[] = {}; std::vector<int> v = vec_transform(a, sizeof(a) / sizeof(int)); HeapSort(v); pass = pass && vec_equal(v, a, sizeof(a) / sizeof(int)); } { int a[] = {1}; std::vector<int> v = vec_transform(a, sizeof(a) / sizeof(int)); HeapSort(v); pass = pass && vec_equal(v, a, sizeof(a) / sizeof(int)); } { int a[] = {1, 2}; std::vector<int> v = vec_transform(a, sizeof(a) / sizeof(int)); HeapSort(v); pass = pass && vec_equal(v, a, sizeof(a) / sizeof(int)); } { int a[] = {2, 1}; std::vector<int> v = vec_transform(a, sizeof(a) / sizeof(int)); HeapSort(v); int b[] = {1, 2}; pass = pass && vec_equal(v, b, sizeof(b) / sizeof(int)); } { int a[] = {9, 8, 7, 6, 5}; std::vector<int> v = vec_transform(a, sizeof(a) / sizeof(int)); HeapSort(v); int b[] = {5, 6, 7, 8, 9}; pass = pass && vec_equal(v, b, sizeof(b) / sizeof(int)); } return pass; }
int main() { //int array[] = {7,6,5,3,4,2,1,10,9,8}; int array[] = {10,9,8,7,6,5,4,3,2,1}; int len = sizeof(array) / sizeof(int); PrintArray(array,len,"堆排序前:"); HeapSort(array,len); printf("\n"); return 0; }
int main() { generateBase(); for (int i = 0; i < limit; ++i) arr[i] = base[i]; HeapSort(arr, arr + limit); for (int i = 0; i < limit - 1; ++i) if (arr[i] > arr[i + 1]) printf("Error\n"); for (int i = 0; i < limit; ++i) arr[i] = base[i]; HeapSort(arr, arr + limit, Greater()); for (int i = 0; i < limit - 1; ++i) if (arr[i] < arr[i + 1]) printf("Error\n"); printf("OK\n"); return 0; }
int main(int argc,const char *argv[]){ int a[] = {3,25,9,30,2}; HeapSort(a, 5); for (int i = 0; i < 5; i++) { printf("%d ",a[i]); } return 0; }
void TestHeapSort() { int array[] = {2,5,6,9,1,3,4,7,8,0}; HeapSort(array,sizeof(array)/sizeof(array[0])); for(int i = 0; i<sizeof(array)/sizeof(array[0]); i++) { cout<<array[i] <<" " ; } cout <<endl; }
int main() { //这里数组下标从1开始,数组第一个元素没有使用 int A[] = {0,-12,5,55,5,4,3,2,1}; int i; HeapSort(A,5); for(i=1;i<=5;i++) printf("%d\n",A[i]); system("pause"); return 0; }
int main() { int nArr[10] = {4,1,3,2,16,9,10,14,8,7}; PrintArr(nArr, 10); HeapSort(nArr, 10); PrintArr(nArr, 10); system("pause"); return 0; }
int main() { int i; scanf("%d", &n); /* to specify how many number user will enter */ for (i = 0; i < n; ++i) scanf("%d", &data[i]); /* takes number from user */ HeapSort(data); for (i = 0; i < n; ++i) printf("%d\n", data[i]); /* print out numbers that are implemented insertion sort */ }
int main(int argc, const char *argv[]) { int a[100]; int n; printf("enter the n:"); scanf("%d", &n); input(a, n); HeapSort(a, n); print(a, n); return 0; }
void Triangles::MakeQuadrangles(double costheta) { if (verbosity>2) cout << " -- MakeQuadrangles costheta = " << costheta << endl; if (verbosity>5) cout << " (in) Nb of Quadrilaterals = " << NbOfQuad << " Nb Of Triangles = " << nbt-NbOutT- NbOfQuad*2 << " Nb of outside triangles = " << NbOutT << endl; if (costheta >1) { if (verbosity>5) cout << " do nothing costheta >1 "<< endl; return;} Int4 nbqq = (nbt*3)/2; DoubleAndInt4 *qq = new DoubleAndInt4[nbqq]; Int4 i,ij; int j; Int4 k=0; for (i=0;i<nbt;i++) for (j=0;j<3;j++) if ((qq[k].q=triangles[i].QualityQuad(j))>=costheta) qq[k++].i3j=i*3+j; // sort qq HeapSort(qq,k); Int4 kk=0; for (ij=0;ij<k;ij++) { // cout << qq[ij].q << " " << endl; i=qq[ij].i3j/3; j=(int) (qq[ij].i3j%3); // optisamition no float computation if (triangles[i].QualityQuad(j,0) >=costheta) triangles[i].SetHidden(j),kk++; } NbOfQuad = kk; if (verbosity>2) { cout << " (out) Nb of Quadrilaterals = " << NbOfQuad << " Nb Of Triangles = " << nbt-NbOutT- NbOfQuad*2 << " Nb of outside triangles = " << NbOutT << endl; } delete [] qq; #ifdef DRAWING2 Draw(); inquire(); #endif }
int main(void) { int arr[4] = {3, 4, 2, 1}; int i; HeapSort(arr, sizeof(arr)/sizeof(int), PriComp); for(i=0; i<4; i++) printf("%d ", arr[i]); printf("\n"); return 0; }
//ソートして、隣の要素が同じかどうか調べる //時間計算量: nlogn + n = O(nlogn) int CheckDuplicatesInArray_2(int A[], int n){ //ヒープソートで整列する HeapSort(A, n); //隣の要素を確認する int i; for(i = 0; i < n - 1; i++){ //一致するか確認 if(A[i] == A[++i]){ return A[i]; } } return 0; }
void main(){ int n,m,i = 1; scanf("%d %d\n",&n,&m); a[0] = n; while(n--){ scanf("%d",&a[i++]); } HeapSort(a,m); for(i = 1;i <= m-1;i++){ printf("%d ",a[a[0] - i + 1]); } printf("%d\n",a[a[0] - i + 1]); }
void MinRange(int arrays[3][5], int m, int n) { int *cur = (int *)malloc(sizeof(int) * m); for (int i = 0; i < m; i++) { cur[i] = 1; } struct ArrayData *heap = (struct ArrayData *)malloc(sizeof(struct ArrayData) * m); for (int i = 0; i < m; i++) { heap[i].array = i; heap[i].data = arrays[i][0]; } HeapSort(heap, 3); int ms = heap[0].data, me = heap[m - 1].data; int ts = 0, te = 0; while (1) { HeapSort(heap, 3); ts = heap[0].data; te = heap[m - 1].data; if (te - ts < me - ms) { me = te; ms = ts; } if (cur[heap[0].array] != n) { heap[0].data = arrays[heap[0].array][cur[heap[0].array]]; cur[heap[0].array] += 1; } else { break; } } printf("[%d, %d]\n", ms, me); }
int main() { int a[]={15,35,25,45,65,55}; /* InsertSort(a,6); BiInsertSort(a,6); */ //BubbleSort(a,6); //SelectSort(a,6); //ShellSort(a,6); //QuickSort(a,0,5); HeapSort(a,6); display(a,6); }
void main(void) { int a[11], i, n = 10; // initialize descending data for (i = 1; i <= 10; i++) a[i] = n - i + 1; HeapSort(a,10); // output sorted data for (i = 1; i <= n; i++) cout << a[i] << ' '; cout << endl; }
int main(){ int x[ ] ={6,1,8,3,10,26,21,45,2,5}; int n=sizeof(x)/sizeof(x[0]); int i; struct nod *root = (struct nod*) malloc (sizeof(struct nod)); root->left = NULL; root->right = NULL; root->data = max(x, n); for(i=0;i<n;i++){ insert(root, x[i]); } HeapSort(root); return 0; }