Anda di halaman 1dari 22

TUGAS PEMOGRAMAN SORTING

Nama Kelompok :
Sulistyo
Zuhairini Azzahra
Almira Mutiara
Sharah Annisa

PRODI MATEMATIKA
FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM
UNIVERSITAS NEGERI JAKARTA
201

BUBBLE SORT

#include <cstdlib>
#include <iostream>
using namespace std;
void print_array(int array[], int size) {
cout<< " bubble sort steps: ";
int j;
for (j=0; j<size; j++)
cout <<" "<< array[j];
cout << endl;
}//end of print_array
void bubble_sort(int arr[], int size) {
bool not_sorted = true;
int j=1,tmp;
while (not_sorted) {
not_sorted = false;
j++;
for (int i = 0; i < size ; i++) {
if (arr[i] > arr[i + 1]) {
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
not_sorted = true;
}//end of if
print_array(arr,size);
}//end of for loop

}//end of while loop


}//end of bubble_sort
int main() {
int n,a[100],i,j,tmp;
printf("---------------- Bubble Sort --------------- \n\n");

cout<<"\nMasukan jumlah data : ";


cin>>n;
for(i=0;i<=n-1;i++)
{
cout<<"\nMasukan data ke : ["<<i<<"]: ";cin>>a[i];
}
cout<<"\n\nData yang sebelum diurutkan : ";
for(i=0;i<=n-1;i++)
{
cout<<" "<<a[i];
}
cout<<endl;
printf("\n\nLangkah-langkah Bubble sort : \n");
cout << "-------------------------\n" ;
bubble_sort(a,n);
cout<<"\n\nData yang setelah diurutkan : ";
print_array(a,n);

cout << "---------------------------------------------\n" ;


system("pause");

return 0;
}//end of main

QUICK SORT

#include <iostream>
#include <iomanip>

using namespace std;

#define size 10

/* swap a[i] and a[j] */


void swap(int a[], int i, int j)
{
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}

void print(const int arr[])


{
for(int j=0;j < size; j++) {
cout << setw(3) << arr[j];
}
cout << endl;
}

/* sort arr[left]...arr[right] into increasing order */


void qsort(int a[], int left_index, int right_index)
{
int left, right, pivot;

if(left_index >= right_index) return;

left = left_index;
right = right_index;

// pivot selection
pivot = a[(left_index + right_index) /2];

// partition
while(left <= right) {
while(a[left] < pivot) left++;
while(a[right] > pivot) right--;
if(left <= right) {
swap(a,left,right);
left++; right--;
}
print(a);
}

// recursion
qsort(a,left_index,right);
qsort(a,left,right_index);
}

int main() {

int n,a[100],i,j;
printf("------------------- QUICK SORT ----------------- \n\n");

cout<<"\nMasukan jumlah data : ";


cin>>n;
for(i=0;i<=n-1;i++)
{
cout<<"\nMasukan data ke : ["<<i<<"]: ";
cin>>a[i];
}
cout<<"\n\nData yang sebelum diurutkan : ";
for(i=0;i<=n-1;i++)
{
cout<<" "<<a[i];
}
cout<<endl;
printf("\nLangkah-langkah Quick sort : \n");

qsort(a,0,size-1);
printf("\n\n Data yang setelah diurutkan :");
print(a);

cout <<
"\n--------------------------------------------------------------------------------" ;
system("pause");
}

INSERTION SORT

#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;
//member function

void insertion_sort(int arr[], int length);


void print_array(int array[],int size);
int main() {
int n,array[10],i,j,tmp;
printf("------------------ Insertion Sort ------------------- \n\n");

cout<<"\nMasukan jumlah data : ";


cin>>n;
for(i=0;i<=n-1;i++)
{
cout<<"\nMasukan data ke : ["<<i<<"]: ";cin>>array[i];
}
cout<<"\n\nData yang sebelum diurutkan : ";
for(i=0;i<=n-1;i++)
{
cout<<" "<<array[i];
}
cout<<endl;
insertion_sort(array,n);
cout<<"\n\nData yang setelah diurutkan : ";
print_array(array,n);
cout << "---------------------------------------------\n" ;
system("pause");
return 0;
}//end of main
void insertion_sort(int arr[], int length)

{
int i, j ,tmp;
for (i = 1; i < length; i++) {
j = i;
while (j > 0 && arr[j - 1] > arr[j]) {
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
}//end of while loop
print_array(arr,length);
}//end of for loop
}//end of insertion_sort.
void print_array(int array[], int size)
{
cout<< "\nsorting: ";
int j;
for (j=0; j<size;j++)
for (j=0; j<size;j++)
cout <<" "<< array[j];
cout << endl;
}//end of print_array

IN PLACE SORT

#include <iostream>

using namespace std;

void swap(int *n, int *m) {

int tmp;
tmp = *m;
*m = *n;
*n = tmp;
}

void print(int a[], int sz) {


for (int i = 0; i < sz; i++ ) cout << a[i] << " ";
cout << endl;
}

void SelectionSort(int a[], int sz) {


int i,j,mn;
/* traverse the entire array */
for (i = 0; i < sz; i++) {
/* find the min */
mn = i;
/* compare against all other elements */
for (j = i + 1; j < sz; j++) {
if (a[j] < a[mn]) mn = j;
}
swap(a[mn], a[i]);
print(a,sz);
}
}

int main() {
int a[20],sz,i,j,mn;
printf("------------------- IN PLACE SORT ------------------\n\n");

cout<<"\nMasukan jumlah data : ";


cin>>sz;
for(i=0;i<=sz-1;i++)
{
cout<<"\nMasukan data ke : ["<<i<<"]: ";
cin>>a[i];
}
cout<<"\n\nData yang sebelum diurutkan : ";
for(i=0;i<=sz-1;i++)
{
cout<<" "<<a[i];
}
cout<<endl;
printf("\nLangkah-langkah In Place sort : \n");

cout << "-------------------------\n" ;


SelectionSort(a, sz);
printf("\n\n Data yang setelah diurutkan :");
print(a,sz);

cout <<
"\n--------------------------------------------------------------------------------" ;

system("pause");
return 0;
}

HEAP SORT

#include <iostream>
#include<iomanip>
#include <conio.h>

using namespace std;


void max_heapsort(int*a, int i, int n)

{
int j, tmp;
tmp = a[i];
j =2*i;
while(j <= n)
{
if(j < n && a[j+1]> a[j])
j = j+1;
if(tmp > a[j])
break;
else if(tmp <= a[j])
{
a[j/2]= a[j];
j =2*j;
}
}
a[j/2]= tmp;
return;
}
void heapsort(int*a, int n)
{
int i, tmp;
for(i = n; i >=2; i--)
{
tmp = a[i];
a[i]= a[1];

a[1]= tmp;
max_heapsort(a, 1, i-1);
cout << "\n\n Heap sort step : "<<"["<<((i-n)*(-1))
+1<<"]"
<<": ";
for(int r=1;r<=n;r++)
cout<<a[r]<< " ";

}
}
void max_heapsort(int*a, int n)
{
int i;
for(i = n/2; i >=1; i--)
{
max_heapsort(a, i, n);
}
}
int main()
{
int a[20],n;
cout<<"--------------- Heap Sort --------------\n\n";

cout<<"\n Masukan Jumlah Data :";


cin>>n;
for(int i=1;i<=n;i++)
{

cout<<"\n Masukan Data ke :["<<i<<"]:";


cin>>a[i];
}
cout<<"\n Data yang sebelum diurutkan :";
for(int i=1;i<=n;i++)
{
cout<<a[i]<<" ";

}
cout<<"\n\n Langkah-langkah Heap Sort : ";
cout<<"\n-----------------------------";
max_heapsort(a,n);
heapsort(a,n);

cout<<"\n";

cout<<"\n\n Data yang setelah diurutkan:";


for(int i=1;i<=n;i++)
{
cout<< a[i]<<" ";

}
_getch();
return 0;
}

RADIX SORT

#include <stdio.h>
#include <iostream.h>
#define MAX 100
#define SHOWPASS
#define BASE 10
void print(int *a, int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d\t", a[i]);

void radixsort(int *a, int n)


{
int i, b[MAX], m = a[0], exp = 1;

//Get the greatest value in the array a and assign it to m


for (i = 1; i < n; i++)
{
if (a[i] > m)
m = a[i];
}

//Loop until exp is bigger than the largest number


while (m / exp > 0)
{
int bucket[BASE] = { 0 };

for (i = 0; i < n; i++)


bucket[(a[i] / exp) % BASE]++;

for (i = 1; i < BASE; i++)


bucket[i] += bucket[i - 1];

for (i = n - 1; i >= 0; i--)


b[--bucket[(a[i] / exp) % BASE]] = a[i];

for (i = 0; i < n; i++)


a[i] = b[i];

exp *= BASE;

#ifdef SHOWPASS
printf("\nPASS

: ");

print(a, n);
#endif
}
}

int main()
{

int i, n,arr[MAX];
printf("------------------- Radix Sort ------------------ \n\n");
cout<<"\nMasukan jumlah data : ";
cin>>n;
n = n < MAX ? n : MAX;

for(i=0;i<n;i++)
{
cout<<"\nMasukan data ke : ["<<i<<"]: ";cin>>arr[i];
}
cout<<"\n\nData yang sebelum diurutkan : ";
for(i=0;i<=n-1;i++)
{
cout<<" "<<arr[i];
}
cout<<endl;

radixsort(&arr[0], n);

printf("\nSORTED : ");
print(&arr[0], n);
printf("\n");

cout<<"\n\nData yang setelah diurutkan : \n";


print(&arr[0], n);

cout << "\n--------------------------------------------------------------------------------" ;


system("pause");
return 0;
}