⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bsort4.txt

📁 冒泡排序
💻 TXT
字号:
//**************************************
//     
// Name: Projectt3
// Description:You might find this usefu
//     l if you are in a C++ class. It's pretty
//     well commented.
//1d array bubble sorting, switch statement menu, generating a random sample dataset. comments welcome.

//
// Returns:arrays, sorted both ways, to 
//     the screen
//
// Assumes:Works with MS VC++ 6.0 sp3

/*
BY:John McDonald
FOR: C++ Programming class
AT:OSU-OKC
WHEN: March 10, 2001
SPECS: Generate a program that accepts up to 50 elements of type double into an array
use a menu
	sort the data for both ascending and descending
	display the results
	also, save the output to a file 
TODO: add user input and save to a file.
*/
#include <iostream.h> // for data streams
#include <stdlib.h>	 // for random
#include <time.h>// seed for rand()

//Constants
		const int MAX = 50; //max array elements
//Global Vars 
		char menuchoice = ' '; 
		double everything[MAX]; //an array of double values
		
		
//Prototypes 
		char get_menu_choice(); //command selection from kbd
		void fillarray();//generate a random dataset
		void sort(double ara[MAX], char ch); //bubble sort an array
		void showarray(); //show the contents of the array;
void main(){ 
	void initializearray();
	while (menuchoice != '4'){ 
		menuchoice = get_menu_choice();


    		switch (menuchoice) {
    			case '1': //enter data
    				fillarray();
    				showarray();
    				break;
    			case '2': //sort the array in ascending order
    				sort(everything,'A');
    				showarray();
    				break;
    			case '3': //sort the array in descending order
    				sort(everything,'D');
    				showarray();
    				break;
    		}//switch
}//while 
}//main 
//display menu and return selection
char get_menu_choice(){ 
char choice = ' '; 
cout << endl << "1. Enter Data" << endl << "2. Sort Ascending" << endl << 
"3. Sort Descending" << endl << "4. Exit" << endl << 
			"--------------------" << endl << "Choose [1-4]>"; 
cin >> choice; 
cin.ignore(100,'\n'); 
return choice; 
} 
//generate a random sample dataset


void fillarray(){
	srand(time(NULL)); //use time for a random number seed


	for(int i=0; i<MAX+1; ++i) {
			everything[i] = (rand() * .001);
	}//for
}//fillarray
//sort the data


void sort(double ara[MAX], char ch){
	int inner, outer;	//loop control
	double temp;		//temp value for swapping
	int didswap;		//1 if a swap took place on each pass


    	for (outer = 0; outer < (MAX-1); outer++){
    		didswap=0;		//initialize after each pass
    		//next loop(s) steps through each pair of values


        		for (inner = outer; inner < MAX; inner++){
        			//perform ascending sort
        			if (ch == 'A'){		
        				if (ara[outer] > ara[inner]){		//first of two is larger
        					temp = ara[outer];
        					ara[outer] = ara[inner];
        					ara[inner] = temp;
        					didswap = 1;		//indicate that a swap occured
        				}//if
        			}//if
        			//perform decending sort
        			if (ch == 'D'){		
        				if (ara[outer] < ara[inner]){		//first of two is larger
        					temp = ara[outer];
        					ara[outer] = ara[inner];
        					ara[inner] = temp;
        					didswap = 1;		//indicate that a swap occured
        				}//if
        			}//if
        		}//for
        		if (!didswap){		//terminate the sort
        			break;
        		}//if
        	}//for
        	return;
    }
    //show the array


        void showarray(){
        		cout << "Here is the array:" << endl;


            		for(int j=0; j<MAX; ++j) {
            			cout << everything[j] << endl; //to standard output
            		
            		}//for
        }
        //initialize the array


            void initializearray(){


                		for(int k=0; k<MAX; ++k) {
                			everything[k] = 3.14159265358979323846;//pi to the 20th decimal place.. 
                		}//for
            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -