bubsort.cpp

来自「数据结构课程程序」· C++ 代码 · 共 65 行

CPP
65
字号
/******Don't forget to download*****
*****GRAPHICAL DATA FILE STRUCTURE*****
*****A approach to learn DFS Graphically*****
Only @  http://www.vivekpatel.cjb.net        */

//Bubble Sort


//Programmed by : Vivek Patel
//URL : www.vivekpatel.cjb.net
//Email : vivek_patel9@rediffmail.com


#include <iostream.h>
#include <conio.h>
#define MAX 10

class bubsort{
	int arr[MAX],n;
	public:
	void getdata();
	void showdata();
	void sortLogic();
};

void bubsort :: getdata(){
	cout<<"How many elements you require : ";
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>arr[i];
}

void bubsort :: showdata(){
	cout<<"\n--Display--\n";
	for(int i=0;i<n;i++)
		cout<<arr[i]<<"   ";
}

void bubsort :: sortLogic(){
	int temp;
	for(int i=0;i<n;i++){
		for(int j=0,exchange=0;j<n;j++){
			if(arr[j] > arr[j+1]){
				temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
				exchange++;
				cout<<"\n arr[j] = "<<arr[j]<<"  arr[j+1] = "<<arr[j+1];
			}
		}
		cout<<endl;
		if(exchange==0)
			break;
	}
}

void main(){
	clrscr();
	cout<<"\n*****Bubble Sort*****\n";
	bubsort obj;
	obj.getdata();
	obj.sortLogic();
	obj.showdata();
	getch();
}

⌨️ 快捷键说明

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