xxxxxxxxxx
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
cout << "Enter N : ";
int n ;
cin >> n ;
int m[n];
int temp = 0 ;
for(int i = 0 ; i < n ; i++)
{
cout << "Enter m [ " << i << "] = ";
cin >> m[i];
}
for(register int i = 0 ; i < n; i++ )
for(register int j = 0 ; j < n-1 ; j++)
if( m[j] > m[j+1] )
{
temp = m[j];
m[j] = m[j+1];
m[j+1] = temp;
}
for(int k = 0 ; k < n ; k++)
cout << m[k] << "\t";
return 0;
}
xxxxxxxxxx
//HOW ABOUT MORE EFFICIENT SOLUTION
// BASIC IDEA- REPEATEDLY SWAP TWO ADJACENT ELEMENTS IF THEY ARE IN A WRONG ORDER.
#include<bits/stdc++.h>
using namespace std;
void bubblesort(int a[], int n)
{
bool swapped=false;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1]) // check if ADJACENT elements are in a wrong order.
{
swap(a[j],a[j+1]); //if they are swap them.
swapped=true;
}
}
if(swapped==false) break; // if for any particular iteration our array doesn't swap--
// -- any numbers then we may conclude that our array has already been sorted. :)
}
}
int main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
bubblesort(a,n);
for(int i=0;i<n;i++) cout<<a[i]<<" ";
return 0;
}
xxxxxxxxxx
#include <iostream>
#include <iomanip>
using namespace std;
void sort (int array[],int size){
for(int i=0; i<size-1; i++){
for(int j=i+1; j<size; j++){
if(array[i] > array[j]){
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
cout<<"Sorted Array: \n";
for(int i=0;i<5;i++){
cout<<setw(5)<<array[i];
}
}
int main()
{
int array [5];
for(int i=0;i<5;i++){
cout<<"Element "<<i<<": ";
cin>>array[i];
}
sort(array,5);
}
xxxxxxxxxx
#include <bits/stdc++.h>
using namespace std;
int main (void) {
int a[] = {5, 4, 3, 2, 1}, tempArr, i, j;
for (i = 0; i < 5; i++) {
for (j = i + 1; j < 5; j++) {
if (a[j] < a[i]) {
tempArr = a[i];
a[i] = a[j];
a[j] = tempArr;
}
}
}
for(i = 0; i < 5; i++) {
cout<<a[i]<<"\n";
}
return 0;
}
xxxxxxxxxx
// Bubble Sort algorithm -> jump to line 21
#include <iostream> // Necessary for input output functionality
#include <bits/stdc++.h> // To simplify swapping process
/**
* Sort array of integers with Bubble Sort Algorithm
*
* @param arr Array, which we should sort using this function
* @param arrSZ The size of the array
* @param order In which order array should be sort
*
* @return Sorted array of integers
*/
void bubbleSortInt(double arr[], int arrSz, string order = "ascending")
{
for (int i = 0; i < arrSz; ++i)
{
for (int j = 0; j < (arrSz - i - 1); ++j)
{
// Swapping process
if ((order == "descending") ? arr[j] < arr[j + 1] : arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
}
}
}
return; // Optional because it's a void function
} // end bubbleSortInt
int main()
{
return 0; // The program executed successfully.
} // end main
xxxxxxxxxx
// hàm sắp xếp nổi bọt (bubble sort)
void BubbleSort(int a[], int n){
int temp; // biến tạm temp
for (int i = 0; i < n; i++){
for (int j = i + 1; j < n; j++){
if (a[j] > a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}