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 argc, char const *argv[]) { int length = atoi(argv[1]); //printf("%s,%s\n",argv[0],argv[1]); int *a = GenerateArray(length); DisplayArray(a, length); quick_sort(a, 0, length - 1); DisplayArray(a, length); //Free(a,length); getchar(); return 0; }
int main() { printf("\nSorting %d Elemetns using merge Sort\n\n",ARRAY_SIZE); int Array[ARRAY_SIZE] ; InitializeArray(Array); printf("\t\t::::::::::::::::Unsorted array:::::::::::::::::::\n\n"); DisplayArray(Array); printf("\n\n" ); int noElements; noElements = sizeof(Array)/sizeof(Array[0]); clock_t start = clock(); mergeSort(Array,noElements); clock_t end = clock(); float seconds = (float)(end - start) / CLOCKS_PER_SEC; printf("\t\t::::::::::::::::Sorted array:::::::::::::::::::\n\n"); DisplayArray(Array); printf("\n\n\t\t::::::::::::::::Time taken - %f sec:::::::::::::::::::\n\n",seconds); return 0; }
int main ( void ) { char cSystem = '0'; int Array1[ 10000 ], Array2[ 10000 ]; printf ( "Welcome to CS460 HW 09 solution!\n" ); for ( cSystem = UserMenu ( &cSystem ); cSystem - 48; cSystem = UserMenu ( &cSystem ) ) { switch ( cSystem - 48 ) { case 1: PopulateArray ( Array1 );//populate array break; case 2: CopyArrays ( Array1, Array2 );//copy array break; case 3: printf ( "Array 1:\n" ); DisplayArray ( Array1 );//display array1 printf ( "\nArray 2:\n" ); DisplayArray ( Array2 );//display array2 printf ( "\n" ); break; case 4: CalculateTime ( Array1 );//calculate bubble sort time break; case 5: printf ( "Quick sort time is %lf\n", QsortTime );//print QsortTime break; } } Exit (); return 0; }
// display variant as a string void DisplayVariant (VARIANT *pvar) { VARIANT vtemp, *ptemp; int vtype; char *str; if (V_ISARRAY(pvar)) { vtype = V_VT(pvar) & ~VT_ARRAY; DisplayArray(V_ARRAY(pvar), vtype); } else { // Display result as a string if (V_VT(pvar) != VT_BSTR) { ::VariantInit(&vtemp); ::VariantChangeType(&vtemp, pvar, 0, VT_BSTR); ptemp = &vtemp; } else { ptemp = pvar; } if (V_VT(ptemp) == VT_BSTR) { str = BstrToCstr(V_BSTR(ptemp)); printf("DADiSP returned: %s\n", str); free(str); // Free the string. if (ptemp == &vtemp) { ::SysFreeString(V_BSTR(ptemp)); } } else { printf("Could Not Display Type %d", V_VT(pvar)); } } }
int ShellSort(int the_array[], unsigned int size)//Apply the shell sort algorithm to sort an array of integers. { int passes = 0; int gap = size / 2; while (gap > 0) { passes++; for (int i = gap; i <= (size - 1); i++) { int temp = the_array[i]; int j = i; while (j >= gap && the_array[j - gap] > temp) { the_array[j] = the_array[j - gap]; j = j - gap; } the_array[j] = temp; DisplayArray(the_array); } gap = gap / 2; } return passes; }
d int main() { int n = 10; cout << "<1>求顺序表中第i个元素(函数),若不存在,报错。" << endl; // 顺序表长度n≥10,i分别为5,n,0,n+1,n+2 SeqList L1_1 = GenerateSeqList(1, 10, n); cout << "第一组数据:"; DisplayArray(L1_1); cout << "i=5 : " << ListGet(&L1_1, 5) << endl; cout << "i=n : " << ListGet(&L1_1, n) << endl; cout << "i=0 : " << ListGet(&L1_1, 0) << endl; cout << "i=n+1: " << ListGet(&L1_1, n + 1) << endl; cout << "i=n+2: " << ListGet(&L1_1, n + 2) << endl; // 顺序表长度n=0,i分别为0,2 SeqList L1_2 = {{}, 0}; cout << "第二组数据:"; DisplayArray(L1_2); cout << "i=0: " << ListGet(&L1_2, 0) << endl; cout << "i=2: " << ListGet(&L1_2, 2) << endl; cout << "\n<2>在第i个结点前插入值为x的结点。" << endl; // 顺序表长度n≥10,i分别为5,n,0,n+1,n+2 cout << "第一组数据:"; DisplayArray(L1_1); int x = 100; cout << "i=5 : "; DisplayArray(ListInsert(L1_1, x, 5)); cout << "i=n : "; DisplayArray(ListInsert(L1_1, x, n)); cout << "i=n+1: "; DisplayArray(ListInsert(L1_1, x, n + 1)); cout << "i=0 : "; DisplayArray(ListInsert(L1_1, x, 0)); cout << "i=1 : "; DisplayArray(ListInsert(L1_1, x, 1)); cout << "i=n+2: "; DisplayArray(ListInsert(L1_1, x, n + 2)); // 顺序表长度n=0,i=5 cout << "第二组数据:"; DisplayArray(L1_2); cout << "i=5: "; DisplayArray(ListInsert(L1_2, x, 5)); cout << "\n<3>删除顺序表中第i个元素结点。" << endl; // 顺序表长度n≥10,i分别为5,n,0,1,n+1,0 SeqList L3_1 = GenerateSeqList(1, 10, n); cout << "第一组数据:"; DisplayArray(L3_1); cout << "i=5 : "; DisplayArray(ListDelete(L3_1, 5)); cout << "i=n : "; DisplayArray(ListDelete(L3_1, n)); cout << "i=1 : "; DisplayArray(ListDelete(L3_1, 1)); cout << "i=n+1: "; DisplayArray(ListDelete(L3_1, n + 1)); cout << "i=0 : "; DisplayArray(ListDelete(L3_1, 0)); // 顺序表长度n=0,i=5 cout << "第二组数据:"; DisplayArray(L1_2); cout << "i=5: "; DisplayArray(ListDelete(L1_2, 5)); cout << "\n<4>在一个递增有序的顺序表L中插入一个值为x的元素,并保持其递增有序特性。" << endl; SeqList L4_1 = {{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}, 10}; cout << "x=25 : "; DisplayArray(IncreaseListInsert(L4_1, 25)); cout << "x=85 : "; DisplayArray(IncreaseListInsert(L4_1, 85)); cout << "x=110: "; DisplayArray(IncreaseListInsert(L4_1, 110)); cout << "x=8 : "; DisplayArray(IncreaseListInsert(L4_1, 8)); cout << "\n<5>将顺序表L中的奇数项和偶数项结点分解开(元素值为奇数、偶数),分别放入新的顺序表中,\n然后原表和新表元素同时输出到屏幕上,以便对照求解结果。" << endl; SeqList OddL, EvenL; cout << "第一组数据:"; SeqList L5_1 = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60}, 15}; DisplayArray(L5_1); ClassifyOddEven(&L5_1, &OddL, &EvenL); cout << "奇数: "; DisplayArray(OddL); cout << "偶数: "; DisplayArray(EvenL); cout << "第二组数据:"; SeqList L5_2 = {{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}, 10}; DisplayArray(L5_2); ClassifyOddEven(&L5_2, &OddL, &EvenL); cout << "奇数: "; DisplayArray(OddL); cout << "偶数: "; DisplayArray(EvenL); cout << "\n<6>求两个递增有序顺序表L1和L2中的公共元素,放入新的顺序表L3中。" << endl; SeqList SameL; cout << "第一组数据:" << endl; SeqList L6_1_1 = {{1, 3, 6, 10, 15, 16, 17, 18, 19, 20}, 10}; SeqList L6_1_2 = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 18, 20, 30}, 13}; DisplayArray(L6_1_1); DisplayArray(L6_1_2); GetSameElements(&L6_1_1, &L6_1_2, &SameL); cout << "合并结果: "; DisplayArray(SameL); cout << "第二组数据:" << endl; SeqList L6_2_1 = {{1, 3, 6, 10, 15, 16, 17, 18, 19, 20}, 10}; SeqList L6_2_2 = {{2, 4, 5, 7, 8, 9, 12, 22}, 8}; DisplayArray(L6_2_1); DisplayArray(L6_2_2); GetSameElements(&L6_2_1, &L6_2_2, &SameL); cout << "合并结果: "; DisplayArray(SameL); cout << "第三组数据:" << endl; SeqList L6_3_1 = {{}, 0}; SeqList L6_3_2 = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 10}; DisplayArray(L6_3_1); DisplayArray(L6_3_2); GetSameElements(&L6_3_1, &L6_3_2, &SameL); cout << "合并结果: "; DisplayArray(SameL); cout << "\n<7>删除递增有序顺序表中的重复元素,并统计移动元素次数,要求时间性能最好。" << endl; unsigned step = 0; cout << "第一组数据: "; SeqList L7_1 = {{1, 2, 3, 4, 5, 6, 7, 8, 9}, 9}; DisplayArray(L7_1); cout << "删除结果: "; DisplayArray(DeleteSame(&L7_1, step), true, false); cout << " step: " << step << endl; cout << "第二组数据: "; SeqList L7_2 = {{1, 1, 2, 2, 2, 3, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9}, 17}; DisplayArray(L7_2); cout << "删除结果: "; DisplayArray(DeleteSame(&L7_2, step), true, false); cout << " step: " << step << endl; cout << "第三组数据: "; SeqList L7_3 = {{1, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9, 9, 9, 9, 9}, 15}; DisplayArray(L7_3); cout << "删除结果: "; DisplayArray(DeleteSame(&L7_3, step), true, false); cout << " step: " << step << endl; return 0; }
//------------------------------------------------------------------------------ // Main Program //------------------------------------------------------------------------------ int main() { // Table dimensions variables int iRow, iColumn; // Get dimensions while(1) { system("cls"); // Clear Screen printf("Give array dimensions.\nRows:"); scanf("%d", &iRow); printf("Columns:"); scanf("%d", &iColumn); if ((iRow > 1 && iRow <= MAX_ROW) && (iColumn > 1 && iColumn <= MAX_COLUMN)) break; } // Create the array int** iArray = Allocate_2D_Array(iRow, iColumn); // Create list's head pointer struct node* pHead = NULL; // Create list's tail pointer struct node* pTail = NULL; // Create the first node which contains the array's dimensions // pHead now points to that node if (! AddNode(&pHead, iRow, iColumn, 0)) exit(1); // Get array values int i, j; for (i = 0; i < iRow; i++) { for (j = 0; j < iColumn; j++) { printf("Give the [%d][%d] element:", i, j); scanf("%d", &iArray[i][j]); // If not a zero value then add node if (iArray[i][j] != 0) { // Locate the tail node and store its address in pTail LocateTail(pHead, &pTail); // Add new node after the last node (tail node) if (! AddNode(&pTail->m_pNext, i, j, iArray[i][j])) exit(1); } } printf("\n"); } // Show msg printf("Single Linked List created successfully\n\n"); int quit = FALSE; while (quit == FALSE) { printf("Press any key to continue..."); getch(); switch(MenuSelection()) { case 1: { DisplayArray(iArray, iRow, iColumn); break; } case 2: { DisplayList(pHead); break; } case 3: { int** iArrayTwo = Allocate_2D_Array(pHead->m_iX_Pos, pHead->m_iY_Pos); for (i = 0; i < pHead->m_iX_Pos; i++) for (j = 0; j < pHead->m_iY_Pos; j++) iArrayTwo[i][j] = 0; struct node* pCurrent = pHead; while (pCurrent != NULL) { if (pCurrent->m_iData != 0) iArrayTwo[pCurrent->m_iX_Pos][pCurrent->m_iY_Pos] = pCurrent->m_iData; pCurrent = pCurrent->m_pNext; } printf("New Array created successfully: \n\n"); DisplayArray(iArrayTwo, pHead->m_iX_Pos, pHead->m_iY_Pos); printf("\nThe program will now exit\n\n"); DeleteDynArray(iArrayTwo, pHead->m_iX_Pos); quit = TRUE; break; } case 4: { quit = TRUE; break; } } } // Free dynamic allocated memory - 2D Arrays + List DeleteDynArray(iArray, iRow); DeleteList(pHead); system("pause"); return 0; }