// 原始快速排序,pivot取最左边的值 static void qsort1(int *data, int left, int right) { if (left < right) { int i = partition1(data, left, right); qsort1(data, left, i - 1); qsort1(data, i + 1, right); } }
void quick_sort(int *arr, int start, int end) { if (start >= end) return; int middle = partition1(arr, start, end); quick_sort(arr, start, middle-1); quick_sort(arr, middle+1, end); }
void quickSort(int arr[],int low ,int high) { int pivotepos; if(low < high) { pivotepos = partition1(arr,low,high); quickSort(arr,low,pivotepos-1); quickSort(arr,pivotepos+1,high); } return ; }
int main(int argc, char *argv[]) { // Initialize POOMA and output stream, using Tester class Pooma::initialize(argc, argv); Pooma::Tester tester(argc, argv); tester.out() << argv[0] << ": Tests of global ID database." << std::endl; tester.out() << "------------------------------------------------" << std::endl; int size = 120; Interval<1> domain(size); UniformGridPartition<1> partition1(Loc<1>(10)); UniformGridLayout<1> layout1(domain,partition1, ReplicatedTag()); UniformGridPartition<1> partition2(Loc<1>(6)); UniformGridLayout<1> layout2(domain,partition2, ReplicatedTag()); std::vector<INode<1> > inodes; GlobalIDDataBase gidStore; UniformGridLayout<1>::const_iterator p = layout1.beginGlobal(); while (p != layout1.endGlobal()) { inodes.push_back(INode<1>(*p, layout1.ID(), &gidStore)); ++p; } int i; int ni = inodes.size(); for (i = 0; i < ni; ++i) { layout2.touches(inodes[i].domain(), std::back_inserter(inodes), TouchesConstructINode<1>(layout2.ID(),inodes[i].key(), &gidStore) ); } inodes.erase(inodes.begin(), inodes.begin() + ni); int gid1,gid2; int gidStore1,gidStore2; for (i = 0; i < inodes.size(); ++i) { gid1 = layout1.globalID(inodes[i].domain().firsts()); gid2 = layout2.globalID(inodes[i].domain().firsts()); gidStore1 = inodes[i].globalID(layout1.ID()); gidStore2 = inodes[i].globalID(layout2.ID()); tester.check(gid1 == gidStore1); tester.check(gid2 == gidStore2); tester.out() << "domain " << inodes[i].domain() << ", key " << inodes[i].key() << ", gid #1 - (" << gid1 << " == " << gidStore1 << ")" << ", gid #2 - (" << gid2 << " == " << gidStore2 << ")" << std::endl; } gidStore.print(tester.out()); tester.out() << "------------------------------------------------" << std::endl; int retval = tester.results("giddatabaseTest"); Pooma::finalize(); return retval; }
void peano::kernel::regulargrid::parallel::tests::SetupPartitionerTest::test2D_12x8() { #ifdef Dim2 tarch::la::Vector<DIMENSIONS,int> domain; assignList(domain) = 12,9; peano::kernel::regulargrid::parallel::SetupPartitioner partition0(domain,8); peano::kernel::regulargrid::parallel::SetupPartitioner partition1(domain,9); peano::kernel::regulargrid::parallel::SetupPartitioner partition2(domain,10); peano::kernel::regulargrid::parallel::SetupPartitioner partition3(domain,11); validateEquals( partition0.getParallelTopology()(0), 4 ); validateEquals( partition0.getParallelTopology()(1), 2 ); validateEquals( partition1.getParallelTopology()(0), 3 ); validateEquals( partition1.getParallelTopology()(1), 3 ); validateEquals( partition2.getParallelTopology()(0), 3 ); validateEquals( partition2.getParallelTopology()(1), 3 ); validateEquals( partition3.getParallelTopology()(0), 3 ); validateEquals( partition3.getParallelTopology()(1), 3 ); tarch::la::Vector<DIMENSIONS,int> expectedSize; tarch::la::Vector<DIMENSIONS,int> expectedOffset; tarch::la::Vector<DIMENSIONS,int> subdomain; assignList(expectedSize) = 4,3; assignList(subdomain) = 0,0; assignList(expectedOffset) = 0,0; validateEquals( partition1.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition2.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition3.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition1.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition2.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition3.getOffsetOfPartition(subdomain), expectedOffset ); assignList(subdomain) = 1,0; assignList(expectedOffset) = 4,0; validateEquals( partition1.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition2.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition3.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition1.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition2.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition3.getOffsetOfPartition(subdomain), expectedOffset ); assignList(subdomain) = 2,0; assignList(expectedSize) = 3,3; assignList(expectedOffset) = 8,0; validateEquals( partition1.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition2.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition3.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition1.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition2.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition3.getOffsetOfPartition(subdomain), expectedOffset ); assignList(subdomain) = 0,1; assignList(expectedSize) = 4,3; assignList(expectedOffset) = 0,3; validateEquals( partition1.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition2.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition3.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition1.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition2.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition3.getOffsetOfPartition(subdomain), expectedOffset ); assignList(subdomain) = 1,1; assignList(expectedOffset) = 4,3; validateEquals( partition1.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition2.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition3.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition1.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition2.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition3.getOffsetOfPartition(subdomain), expectedOffset ); assignList(subdomain) = 2,1; assignList(expectedSize) = 3,3; assignList(expectedOffset) = 8,3; validateEquals( partition1.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition2.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition3.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition1.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition2.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition3.getOffsetOfPartition(subdomain), expectedOffset ); assignList(subdomain) = 0,2; assignList(expectedSize) = 4,2; assignList(expectedOffset) = 0,6; validateEquals( partition1.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition2.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition3.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition1.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition2.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition3.getOffsetOfPartition(subdomain), expectedOffset ); assignList(subdomain) = 1,2; assignList(expectedSize) = 4,2; assignList(expectedOffset) = 4,6; validateEquals( partition1.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition2.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition3.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition1.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition2.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition3.getOffsetOfPartition(subdomain), expectedOffset ); assignList(subdomain) = 2,2; assignList(expectedSize) = 3,2; assignList(expectedOffset) = 8,6; validateEquals( partition1.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition2.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition3.getSizeOfPartition(subdomain), expectedSize ); validateEquals( partition1.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition2.getOffsetOfPartition(subdomain), expectedOffset ); validateEquals( partition3.getOffsetOfPartition(subdomain), expectedOffset ); #endif }
int randomized_partition(int *arr, int start, int end) { int pivot = random()/RAND_MAX *(end - start) + start; exchange(&arr[pivot], &arr[end]); return partition1(arr, start, end); }