Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by registered user nhan ( 5 years ago )
#include <stdio.h>
#include <conio.h>
#include <windows.h>

#define MAX 20

void Input(int &n);
void InputArray(int arr[], int n);
void OutputArray(int arr[], int n);
void IncrementBubbleSort(int arr[], int n);
void DecrementBubbleSort(int arr[], int n);

int main(){
	int arr[MAX];
	int n;
	Input(n);

	printf("\n=====Nhap Mang=====\n\n");
	InputArray(arr, n);

	printf("\n=====Xuat Mang=====\n");
	OutputArray(arr, n);

	printf("\n\n=====Bubble Sort Tang Dan=====\n");
	IncrementBubbleSort(arr, n);
	OutputArray(arr, n);

	printf("\n\n=====Bubble sort Giam Dan=====\n");
	DecrementBubbleSort(arr, n);
	OutputArray(arr, n);

	getch();
	return 0;
}

void Input(int &n){
	do{
		printf("Nhap so phan tu cua mang: ");
		scanf("%d", &n);

		if(n < 1 || n > MAX){
			system("cls");
		}
	}while(n < 1 || n > MAX);
}

void InputArray(int arr[], int n){
	for(int i = 0; i < n; ++i){
		printf("Nhap mang arr[%d] = ", i);
		scanf("%d", &arr[i]);
	}
}

void OutputArray(int arr[], int n){
	for(int i = 0; i < n; ++i){
		printf("\narr[%d] = %d", i, arr[i]);
	}
}

void IncrementBubbleSort(int arr[], int n){
	int temp;
	for(int i = 0; i < n - 1; ++i){
		for(int j = n - 1; j > i; --j){
			if(arr[j] < arr[j - 1]){
				temp = arr[j];
				arr[j] = arr[j - 1];
				arr[j - 1] = temp;
			}
		}
	}
}

void DecrementBubbleSort(int arr[], int n){
	int temp;
	for(int i = 0; i < n - 1; ++i){
		for(int j = n - 1; j > i; --j){
			if(arr[j] > arr[j - 1]){
				temp = arr[j];
				arr[j] = arr[j - 1];
				arr[j - 1] = temp;
			}
		}
	}
}

 

Revise this Paste

Your Name: Code Language: