int main(int argc, char** argv) { int i, n; int* A; clock_t start, end; double elapsed_time, t1, t2; MPI_Init(NULL, NULL); t1 = MPI_Wtime(); A = (int *)malloc(sizeof(int)*N); if (A == NULL) { printf("Fail to malloc\n"); exit(0); } for (i=N-1; i>=0; i--) A[N-1-i] = i; if (isSorted(A, N)) printf("Array is sorted\n"); else printf("Array is NOT sorted\n"); bubbleSort(A, N); printArray(&A[N-10], 10); if (isSorted(A, N)) printf("Array is sorted\n"); else printf("Array is NOT sorted\n"); t2 = MPI_Wtime(); printf( "Elapsed time MPI_Wtime is %f\n", t2 - t1 ); MPI_Finalize(); return 0; }
int test_quicksort(void) { int i, j; int* arr; for (i=1; i<33; i++) { arr = (int*)malloc(sizeof(int)*i); srand((unsigned int)time(NULL)+i); for (j=0; j<i; j++) arr[j] = rand()%100; printf("%d Element Array\n", i); printArr(arr, i); if (isSorted(arr, i)) printf("Sorted Array\n"); else printf("Not Sorted Array\n"); quicksort(arr, 0, i-1); printArr(arr, i); if (isSorted(arr, i)) printf("Sorted Array\n"); else printf("Not Sorted Array\n"); printf("\n"); free(arr); } return 1; }
void testQuickSort(){ int size; int * arr; int sortedArray1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int sortedArray2[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; int size1 = sizeof(sortedArray1)/sizeof(int); int size2 = sizeof(sortedArray2)/sizeof(int); for(size = 1; size < 17; size++) { arr = createArrayRandomly(size); printArray(arr, size); quickSort(arr, 0, size - 1); printf("[after quickSort]"); printArray(arr, size); printf("isSorted: %d\n", isSorted(arr, size)); printf("-------------------------------------------------------\n"); free(arr); } printf("[test of sorted arrays]\n"); printArray(sortedArray1, size1); quickSort(sortedArray1, 0, size1 - 1); printf("[after quickSort]"); printArray(sortedArray1, size1); printf("isSorted: %d\n", isSorted(sortedArray1, size1)); printArray(sortedArray2, size2); quickSort(sortedArray2, 0, size2 - 1); printf("[after quickSort]"); printArray(sortedArray2, size2); printf("isSorted: %d\n", isSorted(sortedArray2, size2)); }
int main() { int testA[] = { 1, 2, 3, 4, 5 }; double testB[] = { 1.1, 3.3, 2.2, 4.4, 5.5 }; std::string testC[] = { "ayy", "bee", "cee" }; std::cout << isSorted(testA, 5) << std::endl; std::cout << isSorted(testB, 5) << std::endl; std::cout << isSorted(testC, 3) << std::endl; }
TEST(Heapsort_normal, DescDouble) { std::array<double ,arraySize> a; Dataset::descArray(a); normal(a); isSorted(a); }
TEST(Quicksort_threeWayPartitioning, RandomString) { std::array<std::string ,arraySize> a; Dataset::randArray(a); threeWayPartitioning(a); isSorted(a); }
TEST(Heapsort_normal, RandomInt) { std::array<int ,arraySize> a; Dataset::randArray(a); normal(a); isSorted(a); }
TEST(Quicksort_withShiftoperator, RandomString) { std::array<std::string ,arraySize> a; Dataset::randArray(a); withShiftoperator(a); isSorted(a); }
TEST(Quicksort_threeWayPartitioning, AscInt) { std::array<int ,arraySize> a; Dataset::ascArray(a); threeWayPartitioning(a); isSorted(a); }
TEST(Quicksort_withShiftoperator, RandomInt) { std::array<int ,arraySize> a; Dataset::randArray(a); withShiftoperator(a); isSorted(a); }
TEST(Quicksort_withShiftoperator, RandomDouble) { std::array<double ,arraySize> a; Dataset::randArray(a); withShiftoperator(a); isSorted(a); }
TEST(Heapsort_normal, RandomDouble) { std::array<double ,arraySize> a; Dataset::randArray(a); normal(a); isSorted(a); }
TEST(Heapsort_normal, RandomString) { std::array<std::string ,arraySize> a; Dataset::randArray(a); normal(a); isSorted(a); }
store::IndexCondition_t IndexImpl::createCondition(store::IndexCondition::Kind k) { if (!isSorted() && (k == store::IndexCondition::BOX_VALUE || k == store::IndexCondition::BOX_GENERAL)) { RAISE_ERROR_NO_LOC(zerr::ZSTR0007_INDEX_UNSUPPORTED_PROBE_CONDITION, ERROR_PARAMS(IndexConditionImpl::getKindString(k), getName()->getStringValue())); } if (isGeneral()) { return new GeneralIndexCondition(this, k); } else if (k == store::IndexCondition::POINT_VALUE) { return new IndexPointCondition(this, k); } else if (k == store::IndexCondition::BOX_VALUE) { return new IndexBoxValueCondition(this, k); } else { RAISE_ERROR_NO_LOC(zerr::ZSTR0007_INDEX_UNSUPPORTED_PROBE_CONDITION, ERROR_PARAMS(IndexConditionImpl::getKindString(k), getName()->getStringValue())); ZORBA_ASSERT(false); } }
// Save an Array to the specified file name void saveArray (char * fname, char * sortAlg, Array A) { FILE * fp; int i; char sname[100]; sprintf(sname, "%s_%s.sorted", sortAlg, fname); if((fp = fopen(sname, "w")) == NULL) // Open .sorted file { perror(""); exit(1); } for(i = 0; i < A.size; i++) // Print each number to the new file { fprintf(fp, "%d\n", *(A.p+i)); } fclose(fp); strcat(sname, ".stats"); if((fp = fopen(sname, "w")) == NULL) // Open .stats file { perror(""); exit(1); } fprintf(fp, "%ld compares\n", compCount); fprintf(fp, "%ld milliseconds\n", sortTime); fclose(fp); if(!isSorted(A)) printf("%s sort did not sort %s correctly\n", sortAlg, fname); }
int* sort(int A[20]) { /* while(!isSorted(A)) { int i; for (i = 0; i<19; i++) { int temp = A[i]; int temp2 = A[i+1]; A[i] = getMin(temp, temp2); A[i+1] = getMax(temp, temp2); } }*/ while(!isSorted(A)) { int i; for (i = 0; i<19; i++) { int temp = A[i]; int temp2 = A[i+1]; A[i] = findMin(A, i, i+1); A[i+1] = getMax(temp, temp2); } } return A; }
void Sorts<T>::bogoSort(T arr[], int size) { while (!isSorted(arr, size)) { shuffle(arr, size); } }
/*----------------------------------------------------------------- * Function: Odd_even_sort * Purpose: Sort list using odd-even transposition sort * In args: n * In/out args: a */ void Odd_even_sort( int a[] /* in/out */, int n /* in */) { int phase, i, temp; for (phase = 0; phase < n; phase++) { if (phase % 2 == 0) { /* Even phase */ for (i = 1; i < n; i += 2) if (a[i - 1] > a[i]) { temp = a[i]; a[i] = a[i - 1]; a[i - 1] = temp; } } else { /* Odd phase */ for (i = 1; i < n - 1; i += 2) if (a[i] > a[i + 1]) { temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } } if (isSorted(a, n)) break; } } /* Odd_even_sort */
TEST(Heapsort_normal, DescString) { std::array<std::string ,arraySize> a; Dataset::descArray(a); normal(a); isSorted(a); }
void Search(int step){ int i,nEstimate; m_nSearch++; //估计这次搜索所需要的最小交换次数 nEstimate = LowerBound(m_ReverseArray,m_nCakeCnt); //超过最大交换次数 退出 if(step+nEstimate > m_nMaxSwap){ return; } //如果已经排完序 直接输出结果 if(isSorted(m_ReverseArray,m_nCakeCnt)){ if(step < m_nMaxSwap){ m_nMaxSwap = step; int i; for(i=0;i<m_nMaxSwap;i++){ m_SwapArray[i] = m_ReverseSwapArray[i]; } } return; } //递归进行翻转 for(i=1;i<m_nCakeCnt;i++){ Reverse(0,i); m_ReverseSwapArray[step] = i; Search(step+1); Reverse(0,i); } }
TEST(Heapsort_normal, AscInt) { std::array<int ,arraySize> a; Dataset::ascArray(a); normal(a); isSorted(a); }
/* * don't forget: it is OK to try to remove an element * that is NOT in the set. * If 'x' is not in the set 'self', then * removeSet should do nothing (it's not an error) * Otherwise, ('x' IS in the set), remove x. Be sure to update self->length * It is not necessary (nor recommended) to call malloc -- if removing an element means the * array on the heap is "too big", that's almost certainly OK, and reallocating a smaller array * is almost definitely NOT worth the trouble * * My notes: if the set is empty, don't do anything because it's impossible to remove from a * set with no members in it. If set is singleton, quicker to just do it manually. * * param self: set to remove element 'x,' assuming 'x' is in the set. * param x: member to remove from set 'self' * return: N/A */ void removeSet(Set* self, int x) { if ((!isMemberSet(self, x)) || (isEmptySet(self)) || (!isSorted(self))) { return; } if (self->len == 1) { /* easier to manually remove for a singleton set */ self->elements[0] = 0; self->len = 0; return; } int deleteHere = 0; for (int index = 0; index < self->len; index++) { /* determine index where 'x' is within set */ if (self->elements[index] == x) { deleteHere = index; } } for (int index = deleteHere; index < self->len; index++) { /* O(N). */ if (index != (self->len - 1)) { /* don't want to write potentially random value to last member spot */ self->elements[index] = self->elements[index + 1]; /* shift every member to left one to update set members */ } else { self->elements[index] = 0; /* index reached last member spot. Make as empty spot */ } } self->len = self->len - 1; /* update length of set */ }
TEST(Quicksort_withShiftoperator, DescDouble) { std::array<double ,arraySize> a; Dataset::descArray(a); withShiftoperator(a); isSorted(a); }
/** * Remove all elements from 'self' that are not also elements of 'other.' * If 'self' is empty or identical to 'other' or if 'other' is empty, do nothing. * Update the length of 'self.' * * param self: set to store intersection of 'self' and 'other' in. * param other: set to check against 'self' for any matching members. * return: N/A */ void intersectFromSet(Set* self, const Set* other) { if ((isEqualToSet(self, other)) || (self->len == 0) || (!isSorted(self))) { return; } if (other->len == 0) { /* the intersection of any set with an empty set is an empty set */ self->len = 0; return; } int selfIndex = 0; int otherIndex = 0; int intersectIndex = 0; while (selfIndex < self->len && otherIndex < other->len) { if (self->elements[selfIndex] == other->elements[otherIndex]) { self->elements[intersectIndex] = self->elements[selfIndex]; /* should only add a member if present in both sets */ intersectIndex++; selfIndex++; otherIndex++; } else if (self->elements[selfIndex] > other->elements[otherIndex]) { /* member may be in set but at different index, or not in both sets. Don't add. */ otherIndex++; } else if (self->elements[selfIndex] < other->elements[otherIndex]) { /* member may be in set but at different index, or not in both sets. Don't add. */ selfIndex++; } } self->len = intersectIndex; /* update new length of self */ }
TEST(Quicksort_withShiftoperator, DescString) { std::array<std::string ,arraySize> a; Dataset::descArray(a); withShiftoperator(a); isSorted(a); }
void test_sorter(__sorter sorter, void * const start, void * const end, size_t size, __comporator cmp) { BM.start(); (sorter)(start, end, size, cmp); cout << BM.getTime() << "\t"; EXPECT_TRUE(isSorted(start, end,size,cmp)); }
TEST(Quicksort_withShiftoperator, AscInt) { std::array<int ,arraySize> a; Dataset::ascArray(a); withShiftoperator(a); isSorted(a); }
void Sorts<T>::bogoSort(T arr[], int size){ std::default_random_engine generator(time(nullptr)); while(!isSorted(arr, size)){ shuffle(arr, size, generator); } assert(Sorts<T>::isSorted(arr, size)); }
int main() { //Creater pointer array using malloc and populate it. int *elements; elements = (int*)malloc(sizeOfArray*sizeof(int)); printf("\nPopulating array\n"); for (int i = 0; i < sizeOfArray; ++i){ elements[i] = rand() % randMax; } //Sort them sort(elements); if (isSorted(elements)){ printf("\nArray Sorted \n"); } else{ printf("Not sorted \n"); } printf("Final sequence "); for (int i = 0; i < sizeOfArray; i++){ printf(" %d,", elements[i]); } free(elements); getchar(); return 0; }
TEST(Quicksort_threeWayPartitioning, DescString) { std::array<std::string ,arraySize> a; Dataset::descArray(a); threeWayPartitioning(a); isSorted(a); }