Skip to content

Instantly share code, notes, and snippets.

@amirulasyraf88
Created August 20, 2015 02:44
Show Gist options
  • Save amirulasyraf88/c8555f8aabff34ea3de8 to your computer and use it in GitHub Desktop.
Save amirulasyraf88/c8555f8aabff34ea3de8 to your computer and use it in GitHub Desktop.
Sorting Algorithm [Array]
#include <iostream>
#include <iomanip>
using namespace std;
typedef int DataType;
const int N_ITEMS = 10;
void swap( DataType&, DataType& );
void displayArray( const DataType[], int );
void bubbleSort( DataType[], int );
int main(){
DataType a[ N_ITEMS ] = { 10, 5, 21, 5, 99, 8, 16, 4, 72, 25 };
cout << "Initial array : ";
displayArray( a, N_ITEMS );
cout << endl;
bubbleSort( a, N_ITEMS );
cout << "Sorted array : ";
displayArray( a, N_ITEMS );
cout << endl;
cin.get();
return 0;
}
void swap(DataType& x, DataType& y){
DataType temp = x;
x = y;
y = temp;
cout << "Swapped " << setw(2) << x << " with " << setw(2) << y << " --->";
}
void displayArray( const DataType theArray[], int size ){
for ( int i = 0; i < size; ++i )
cout << setw(2) << theArray[ i ] << " ";
}
void bubbleSort(DataType theArray[], int n){
//To do: Implement bubble sort
bool sorted;
for(int pass=1;(pass<n)&&!sorted;++pass){
sorted=true;
for(int index=0;index<n-pass;++index){
if (theArray[index]>theArray[index+1]){
swap(theArray[index],theArray[index+1]);
sorted=false;
displayArray(theArray,n);
cout<<endl;
}
}
}
}
#include <iostream>
#include <iomanip>
using namespace std;
typedef int DataType;
const int N_ITEMS = 10;
void insertionSort( DataType theArray[], int n );
void displayArray( const DataType theArray[], int first, int last );
int main(){
DataType a[ N_ITEMS ] = { 10, 5, 21, 5, 99, 8, 16, 4, 72, 25 };
cout << "Initial array : ";
displayArray( a, 0, N_ITEMS-1 );
cout << endl;
insertionSort( a, N_ITEMS );
cout << "Sorted array : ";
displayArray( a, 0, N_ITEMS-1 );
cout << endl;
return 0;
}
void displayArray( const DataType theArray[], int first, int last ){
for ( int i = first; i <= last; ++i )
cout << setw(2) << theArray[ i ] << " ";
}
void insertionSort(DataType theArray[], int n){
// unsorted = first index of the unsorted region,
// loc = index of insertion in the sorted region,
// nextItem = next item in the unsorted region
// To do: Implement insertion sort
for(int unsorted=1;unsorted<n;++unsorted){
DataType nextItem=theArray[unsorted];
int loc=unsorted;
while((loc>0)&&(theArray[loc-1]>nextItem)){
theArray[loc]=theArray[loc-1];
loc=loc-1;
}
//insertion next item sorted region
theArray[loc]=nextItem;//insertion
cout<<"insert"<<setw(2)<<nextItem<<"into location"<<setw(2)<<loc<<"-->";
//display
displayArray(theArray,0,unsorted);cout<<"|";
displayArray(theArray,unsorted+1,n-1);
cout<<endl;
}
}
#include <iostream>
#include <iomanip>
using namespace std;
typedef int DataType;
const int N_ITEMS = 10;
void swap( DataType&, DataType& );
void displayArray( const DataType[], int );
int indexOfLargest( const DataType[], int );
void selectionSort( DataType[], int );
int main(){
DataType a[ N_ITEMS ] = { 10, 5, 21, 5, 99, 8, 16, 4, 72, 25 };
cout << "Initial array : ";
displayArray( a, N_ITEMS );
cout << endl;
selectionSort( a, N_ITEMS );
cout << "Sorted array : ";
displayArray( a, N_ITEMS );
cout << endl;
cin.get();
return 0;
}
void swap(DataType& x, DataType& y){
DataType temp = x;
x = y;
y = temp;
cout << "Swapped " << setw(2) << x << " with " << setw(2) << y << " --->";
}
void displayArray( const DataType theArray[], int size ){
for ( int i = 0; i < size; ++i )
cout << setw(2) << theArray[ i ] << " ";
}
int indexOfLargest(const DataType theArray[], int last){
//To do: Get the index of the largest element in the
// unsorted list.
int indexSoFar=0;
for (int currentIndex=1;currentIndex<=last;++currentIndex){
if (theArray[currentIndex]>theArray[indexSoFar])
indexSoFar=currentIndex;
}
return indexSoFar;
}
void selectionSort(DataType theArray[], int n){
//To do: Implement selection sort
for(int last=n-1;last>=1;--last){
int largest=indexOfLargest(theArray,last);
swap(theArray[largest],theArray[last]);
displayArray(theArray,n);
cout<<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment