- Home »
- SCRIPT C++ PENGURUTAN (SORTING)
Unknown
On Minggu, 12 April 2015
1. Insertion Sort
#include <conio.h>
using namespace std;
int main()
{
int a[10], i, j, k, temp;
cout<<"masukan angka:\n";
for (i = 0; i < 16; i++)
{
}
for (i = 1; i < 10; i++)
{
for (j = i; j >= 1; j--)
{
if (a[j] < a[j-1])
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
else
break;
}
}
cout<<"sorted array\n"<<endl;
for (k = 0; k < 16; k++)
{
cout<<a[k]<<endl;
}
getch();
}
2.Tree Sort
#include<iostream>
using namespace std;
struct tree{
int info;
tree *Left, *Right;
};
tree *root;
class TreeSort{
public:
int no_of_elements;
int elements[10];
public:
void getarray();
void sortit();
void insert1(int);
tree *insert2(tree *, tree *);
void display(tree *);
};
void TreeSort::getarray(){
cout<<"Masukkan elemen yang anda inginkan:";
cin>>no_of_elements;
cout<<"masukan angka: \n";
for(int i=0;i<no_of_elements;i++){
cin>>elements[i];
}
}
void TreeSort::sortit(){
for(int i = 0; i < no_of_elements; i++){
insert1(elements[i]);
}
}
tree* TreeSort::insert2(tree *temp,tree *newnode){
if(temp==NULL){
temp=newnode;
}
else if(temp->info < newnode->info){
insert2(temp->Right,newnode);
if(temp->Right==NULL)
temp->Right=newnode;
}
else{
insert2(temp->Left,newnode);
if(temp->Left==NULL)
temp->Left=newnode;
}
return temp;
}
void TreeSort::insert1(int n){
tree *temp=root,*newnode;
newnode=new tree;
newnode->Left=NULL;
newnode->Right=NULL;
newnode->info=n;
root=insert2(temp,newnode);
}
/* Inorder traversal */
void TreeSort::display(tree *t = root){
if(root==NULL){
cout<<"Nothing to display";
}else
if(t!=NULL){
display(t->Left);
cout<<t->info<<" ";
display(t->Right);
}
}
int main(){
TreeSort TS;
TS.getarray();
TS.sortit();
TS.display();
return 0;
}
3. Bubble Sort
#include<iostream>
using namespace std;
int main(){
//declaring array
int array[5];
cout<<"masukan acak 5 angka: "<<endl;
for(int i=0; i<5; i++)
{
//Taking input in array
cin>>array[i];
}
cout<<endl;
cout<<"Input array : "<<endl;
for(int j=0; j<5; j++)
{
//Displaying Array
cout<<"\t\t\tValue at "<<j<<" Index: "<<array[j]<<endl;
}
cout<<endl;
// Bubble Sort Starts Here
int temp;
for(int i2=0; i2<=4; i2++)
{
for(int j=0; j<4; j++)
{
//Swapping element in if statement
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
// Displaying Sorted array
cout<<" Sorted Array : "<<endl;
for(int i3=0; i3<5; i3++)
{
cout<<"\t\t\tValue at "<<i3<<" Index: "<<array[i3]<<endl;
}
return 0;
}
4. Exchange Sort
using namespace std;
#include<iostream>
int main(void)
{
int array[5]; // An array of integers.
int length = 5; // Lenght of the array.
int i, j;
int temp;
//Some input
for (i = 0; i < 5; i++)
{
cout << "Masukan angka: ";
cin >> array[i];
}
//Algorithm
for(i = 0; i < (length -1); i++)
{
for (j=(i + 1); j < length; j++)
{
if (array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
//Some output
for (i = 0; i < 5; i++)
{
cout << array[i] << endl;
}
}
5. Selection Sort
#include<iostream>
#include<conio.h>
using namespace std;
template <class T>
void s_sort(T a[],int n)
{
int i,j,t;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i]) //for descending order use if(a[j]>a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
int main()
{
int a[100],i,n;
cout<<"Masukan jumlah elemen:\n";
cin>>n;
cout<<"\nmasukan angka:\n";
for(i=0;i<n;i++)
{
cout<<"\nmasukan:";
cin>>a[i];
}
s_sort(a,n);
cout<<"\nAfter Sorting\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
getch();
return 0;
}
6. Heap Sort
#include <iostream>
#include <conio.h>
using namespace std;
void max_heapify(int *a, int i, int n)
{
int j, temp;
temp = a[i];
j = 2*i;
while (j <= n)
{
if (j < n && a[j+1] > a[j])
j = j+1;
if (temp > a[j])
break;
else if (temp <= a[j])
{
a[j/2] = a[j];
j = 2*j;
}
}
a[j/2] = temp;
return;
}
void heapsort(int *a, int n)
{
int i, temp;
for (i = n; i >= 2; i--)
{
temp = a[i];
a[i] = a[1];
a[1] = temp;
max_heapify(a, 1, i - 1);
}
}
void build_maxheap(int *a, int n)
{
int i;
for(i = n/2; i >= 1; i--)
{
max_heapify(a, i, n);
}
}
int main()
{
int n, i, x;
cout<<"Masukan jumlah elemen:\n";
cin>>n;
int a[20];
for (i = 1; i <= n; i++)
{
cout<<"masukan angka:"<<(i)<<endl;
cin>>a[i];
}
build_maxheap(a,n);
heapsort(a, n);
cout<<"sorted output\n";
for (i = 1; i <= n; i++)
{
cout<<a[i]<<endl;
}
getch();
}
7. Quick Sort
#include <iostream>
void quickSort(int a[], int first, int last);
int pivot(int a[], int first, int last);
void swap(int& a, int& b);
void swapNoTemp(int& a, int& b);
void print(int array[], const int& N);
using namespace std;
int main()
{
int test[] = { 7, -13, 1, 3, 10, 5, 2, 4 };
int N = sizeof(test)/sizeof(int);
cout << "Size of test array :" << N << endl;
cout << "Before sorting : " << endl;
print(test, N);
quickSort(test, 0, N-1);
cout << endl << endl << "After sorting : " << endl;
print(test, N);
return 0;
}
/**
* Quicksort.
* @param a - The array to be sorted.
* @param first - The start of the sequence to be sorted.
* @param last - The end of the sequence to be sorted.
*/
void quickSort( int a[], int first, int last )
{
int pivotElement;
if(first < last)
{
pivotElement = pivot(a, first, last);
quickSort(a, first, pivotElement-1);
quickSort(a, pivotElement+1, last);
}
}
/**
* Find and return the index of pivot element.
* @param a - The array.
* @param first - The start of the sequence.
* @param last - The end of the sequence.
* @return - the pivot element
*/
int pivot(int a[], int first, int last)
{
int p = first;
int pivotElement = a[first];
for(int i = first+1 ; i <= last ; i++)
{
/* If you want to sort the list in the other order, change "<=" to ">" */
if(a[i] <= pivotElement)
{
p++;
swap(a[i], a[p]);
}
}
swap(a[p], a[first]);
return p;
}
/**
* Swap the parameters.
* @param a - The first parameter.
* @param b - The second parameter.
*/
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
/**
* Swap the parameters without a temp variable.
* Warning! Prone to overflow/underflow.
* @param a - The first parameter.
* @param b - The second parameter.
*/
void swapNoTemp(int& a, int& b)
{
a -= b;
b += a;// b gets the original value of a
a = (b - a);// a gets the original value of b
}
/**
* Print an array.
* @param a - The array.
* @param N - The size of the array.
*/
void print(int a[], const int& N)
{
for(int i = 0 ; i < N ; i++)
cout << "array[" << i << "] = " << a[i] << endl;
}
8. Merge Sort
using namespace std;
#include <iostream>
#include <conio.h>
void merge(int *,int, int , int );
void mergesort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
}
return;
}
void merge(int *a, int low, int high, int mid)
{
int i, j, k, c[50];
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
c[k] = a[i];
k++;
i++;
}
else
{
c[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
c[k] = a[i];
k++;
i++;
}
while (j <= high)
{
c[k] = a[j];
k++;
j++;
}
for (i = low; i < k; i++)
{
a[i] = c[i];
}
}
int main()
{
int a[20], i, b[20];
cout<<"enter the elements\n";
for (i = 0; i < 5; i++)
{
cin>>a[i];
}
mergesort(a, 0, 4);
cout<<"sorted array\n";
for (i = 0; i < 5; i++)
{
cout<<a[i];
}
cout<<"\nenter the elements\n";
for (i = 0; i < 5; i++)
{
cin>>b[i];
}
mergesort(b, 0, 4);
cout<<"sorted array\n";
for (i = 0; i < 5; i++)
{
cout<<b[i];
}
getch();
}
9. Shell Sort
using namespace std;
#include <iostream>
#include <conio.h>
void read(int a[10], int n){
cout<<"Reading\n";
for(int i = 0; i < n; i++)
cin>>a[i];
}
void display(int a[10], int n){
for(int i = 0; i < n; i++)
cout<<a[i]<<"\t";
}
void shell(int a[10], int n){
int gap = n/2;
do{
int swap;
do{
swap = 0;
for(int i = 0; i < n - gap; i++)
if(a[i] > a[i+gap])
{
int t = a[i];
a[i] = a[i+gap];
a[i+gap] = t;
swap = 1;
}
}
while(swap);
}
while(gap = gap / 2);
}
int main(){
int a[10];
cout<<"enter n\n";
cin>>n;
read(a,n);
cout<<"before sorting\n";
display(a,n);
shell(a,n);
cout<<"\nafter sorting\n";
display(a,n);
getch();
}
Senin, 13 April 2015