Assalamu'alaikum...
Menggunakan C++ :
#include<iostream>
#include<iomanip>
using namespace std;
main()
{
int byk, htg=1, tmp, data[15];
cout << "Banyak data : ";
cin >> byk;
for(int i=0; i<byk; i++)
{
cout << "Masukkan data ke-"<<i<<" : ";
cin >> data[i];
}
endl(cout);
cout<<"data diurut (Naik): ";
for(int a=0; a<byk; a++)
{
for(int b=byk-1; b>=a; b--)
{
if(data[b]<data[b-1])
{
tmp = data[b];
data[b] = data[b-1];
data[b-1] = tmp;
}
}
cout<<data[a]<<" ";
}
cout<<"\nData diurut (Turun) : ";
for(int i=byk-1; i<=0; i--)
{
for(int h=0; h<i; h++)
{
if(data[h] > data[h+1])
{
tmp = data[h];
data[h] = data[h+1];
data[h+1] = tmp;
}
}
cout<<data[i]<<" ";
}
cout<<"\n"<<endl;
return 0;
}
C++ ke2 :
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
//Bucket Sort
void bucket_sort (int arr[], int n)
{
//Here range is [1,100]
int m = 101;
//Create m empty buckets
int buckets[m];
//Intialize all buckets to 0
for (int i = 0; i < m; ++i)
buckets[i] = 0;
//Increment the number of times each element is present in the input
//array. Insert them in the buckets
for (int i = 0; i < n; ++i)
++buckets[arr[i]];
//Sort using insertion sort and concatenate
for (int i = 0, j = 0; j < m; ++j)
for (int k = buckets[j]; k > 0; --k)
arr[i++] = j;
}
//Driver function to test above function
int main()
{
int input_ar[] = {10, 24, 22, 62, 1, 50, 100, 75, 2, 3};
int n = sizeof (input_ar) / sizeof (input_ar[0]);
bucket_sort (input_ar, n);
cout << "Sorted Array : " << endl;
for (int i = 0; i < n; ++i)
cout << input_ar[i] << " ";
return 0;
}
Wasalam.
Tidak ada komentar:
Posting Komentar