求助双排列归序
程序代码:
#include "stdafx.h" #include <cstdlib> #include <iostream> #include <iomanip> #include "SortTest.h" using namespace std; int main() { CSortTest MyTest(7,7); MyTest.Populate(); MyTest.DisplayArray(); system("pause"); MyTest.SortAfterCols(); MyTest.Sort(); MyTest.DisplayArray(); system("pause"); return 0; } // SortTest.h #include <iostream> #include <iomanip> using namespace std; class CSortTest { public: CSortTest() { } CSortTest(int A, int B) { Row = A; Col = B; } void Populate() { int i, j, HideFirstCol, HideAfterCols; srand( Row ); i = 0; while ( i < Row ) { HideFirstCol = rand(); if ( HideFirstCol > 0 && HideFirstCol < 68 ) { Numbers[i][0] = HideFirstCol + 93000; j = 1; while ( j < Col ) { HideAfterCols = rand(); if ( HideAfterCols > 9 && HideAfterCols < 501 ) { Numbers[i][j] = HideAfterCols; j++; } } i++; } } } void Sort() { Populate(); int i, j, HideFirstCol, Swap1, Swap2; for ( i = 0; i < Row; i++ ) { Swap1 = i; for ( j = 0; j < Col; j++ ) { HideFirstCol = Numbers[i][j]; Swap2 = j; while ( ( Swap1 > 0 ) && ( Numbers[ Swap1 -1 ][0] > HideFirstCol ) ) { Numbers[ Swap1 ][ Swap2 ] = Numbers[ Swap1 - 1 ][ Swap2 ]; Swap1--; } Numbers[ Swap1 ][ Swap2 ] = HideFirstCol; } } } void SortAfterCols() { int i, j, HideAfterCols, Swap; for ( i = 0; i < Row; i++ ) { for ( j = 0; j < Col; j++ ) { HideAfterCols = Numbers[i][j+1]; Swap = j+1; while ( ( Swap > 0 ) && ( Numbers[i][ Swap - 1 ] > HideAfterCols ) ) { Numbers[i][ Swap ] = Numbers[i][ Swap - 1 ]; Swap--; } Numbers[i][Swap] = HideAfterCols; } } } void DisplayArray() { int i, j; for ( i = 0; i < Row; i++ ) { for ( j = 0; j < Col; j++ ) { cout << setw(5) << Numbers[i][j]; } cout << "\n\n"; } } private: int Row; int Col; int Numbers[10][10]; };归序后我的 AfterCols 为什么没有和 FirstCol 一起走呢? 恩……走是走了,但走错了, 请大家帮我看看, 谢谢!
[ 本帖最后由 obdi00 于 2011-3-28 13:53 编辑 ]