Uploaded on


  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. // Assignment: Sorting using bubble sort, selection sort and insertion sort// Author: Ayesha Saif Bhatti// Class: BSE 3A// Date: 15-12-2012#include "stdafx.h"#include <iostream>#include <time.h>#include <conio.h>using namespace std;//Fucntions implementing sorting algorithmsvoid bubbleSort(int myArray[], int size);void insertionSort(int myArray[], int size);void selectionSort(int myArray[], int size);//Utility functionsvoid generateArray(int myArray[], int size);void displayArray(int myArray[], int size);void copyArray(int dest[], int source[],int size);int _tmain(int argc, _TCHAR* argv[]){ const int size = 1000; int theArray1[size]; //Declare three arrays, each array will have the sameunsorted values int theArray2[size]; int theArray3[size]; time_t start; time_t end; time_t elapsed; generateArray(theArray1,size); //Generate the array of random numbers cout<<"The original unsorted array is: "<< endl; displayArray(theArray1,size); copyArray(theArray2,theArray1, size); //Copy the same values to the two otherarrays copyArray(theArray3,theArray1, size); // ******** Bubble Sort *********/ start = time(NULL); bubbleSort(theArray1, size); //Call the bubble sort function end = time(NULL) ; elapsed = end - start; cout<<"After bubble sort, the sorted array is: "<< endl; displayArray(theArray1,size); //Display the sorted array cout <<"Time taken by bubble sort: " << elapsed * 1000 << " milliseconds." << endl<< endl; // ******** Insertion Sort *********/ start = time(NULL);
  • 2. insertionSort(theArray2, size); //Call the insertion sort function end = time(NULL); elapsed = end - start; cout<<"After insertion sort, the sorted array is: "<< endl; displayArray(theArray2,size); //Display the sorted array cout <<"Time taken by insertion sort: " << elapsed * 1000 << " milliseconds." <<endl << endl; // ******** Selection Sort *********/ start = time(NULL); selectionSort(theArray3, size); //Call selection sort function end = time(NULL); elapsed = end - start; cout<<"After selection sort, the sorted array is: "<< endl; displayArray(theArray3,size); //Display sorted array cout <<"Time taken by selection sort: " << elapsed * 1000 << " milliseconds."<<endl << endl; getch(); return 0;}//////////////////////////////////Function to Display the Array////////////////////////////////void displayArray(int myArray[], int size){ for(int i=0;i<size;i++) cout<< myArray[i] <<" "; cout<< endl;}//////////////////////////////////Bubble Sort////////////////////////////////void bubbleSort(int myArray[], int size){ int temp; bool switched = true; for (int pass = 0; pass < size-1 && switched == true; pass++) { switched = false; for (int j = 0; j < size-pass-1; j++) { if (myArray[j] > myArray[j+1]) // elements out of order { switched = true; temp = myArray[j]; myArray[j] = myArray [j+1]; myArray[j+1] = temp; } // end if } // end inner for loop } // end outer for loop}//////////////////////////////////Insertion Sort////////////////////////////////
  • 3. void insertionSort(int myArray[], int size){ int key; for (int j=1;j<size;j++) { key = myArray[j]; int i = j-1; while(i>=0 && myArray[i]>key) { myArray[i+1]=myArray[i]; i--; } myArray[i+1]=key; }}//////////////////////////////////Selection Sort////////////////////////////////void selectionSort(int myArray[], int size){ int temp; for (int i=0;i<size-1;i++) { int min = i; for(int j=i+1;j<size;j++) if(myArray[j] < myArray[min]) min =j; temp = myArray[i]; myArray[i] = myArray[min]; myArray[min]= temp; }}//////////////////////////////////Generate array of random numbers////////////////////////////////void generateArray(int myArray[], int size){ srand(time(0)); for(int i=0;i<size;i++) myArray[i]= rand()% size;}//////////////////////////////////Copy elements of an array into// another array////////////////////////////////void copyArray(int destination[], int source[], int size){ for(int i=0;i<size;i++) destination[i]= source[i];}