SlideShare a Scribd company logo
//   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 algorithms
void bubbleSort(int myArray[], int size);
void insertionSort(int myArray[], int size);
void selectionSort(int myArray[], int size);

//Utility functions
void 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 same
unsorted 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 other
arrays
         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);
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
////////////////////////////////
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];
}

More Related Content

What's hot

Rust ⇋ JavaScript
Rust ⇋ JavaScriptRust ⇋ JavaScript
Rust ⇋ JavaScript
Ingvar Stepanyan
 
Extreme JavaScript Performance
Extreme JavaScript PerformanceExtreme JavaScript Performance
Extreme JavaScript Performance
Thomas Fuchs
 
Better Software: introduction to good code
Better Software: introduction to good codeBetter Software: introduction to good code
Better Software: introduction to good codeGiordano Scalzo
 
C++ 11 usage experience
C++ 11 usage experienceC++ 11 usage experience
C++ 11 usage experience
GlobalLogic Ukraine
 
Deferred
DeferredDeferred
Deferred
daiying-zhang
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
JungMinSEO5
 
Building fast interpreters in Rust
Building fast interpreters in RustBuilding fast interpreters in Rust
Building fast interpreters in Rust
Ingvar Stepanyan
 
Developing iOS apps with Swift
Developing iOS apps with SwiftDeveloping iOS apps with Swift
Developing iOS apps with Swift
New Generation Applications
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
Lin Yo-An
 
Stupid Awesome Python Tricks
Stupid Awesome Python TricksStupid Awesome Python Tricks
Stupid Awesome Python Tricks
Bryan Helmig
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
Anıl Sözeri
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to Swift
Giordano Scalzo
 
Modern JS with ES6
Modern JS with ES6Modern JS with ES6
Modern JS with ES6
Kevin Langley Jr.
 
Rust言語紹介
Rust言語紹介Rust言語紹介
Rust言語紹介
Paweł Rusin
 
A Few Interesting Things in Apple's Swift Programming Language
A Few Interesting Things in Apple's Swift Programming LanguageA Few Interesting Things in Apple's Swift Programming Language
A Few Interesting Things in Apple's Swift Programming Language
SmartLogic
 
Swiftの関数型っぽい部分
Swiftの関数型っぽい部分Swiftの関数型っぽい部分
Swiftの関数型っぽい部分
bob_is_strange
 
Алексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhereАлексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhere
Sergey Platonov
 
Rust concurrency tutorial 2015 12-02
Rust concurrency tutorial 2015 12-02Rust concurrency tutorial 2015 12-02
Rust concurrency tutorial 2015 12-02
nikomatsakis
 
EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is here
Sebastiano Armeli
 

What's hot (20)

Rust ⇋ JavaScript
Rust ⇋ JavaScriptRust ⇋ JavaScript
Rust ⇋ JavaScript
 
Extreme JavaScript Performance
Extreme JavaScript PerformanceExtreme JavaScript Performance
Extreme JavaScript Performance
 
Better Software: introduction to good code
Better Software: introduction to good codeBetter Software: introduction to good code
Better Software: introduction to good code
 
C++ 11 usage experience
C++ 11 usage experienceC++ 11 usage experience
C++ 11 usage experience
 
Deferred
DeferredDeferred
Deferred
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
 
Building fast interpreters in Rust
Building fast interpreters in RustBuilding fast interpreters in Rust
Building fast interpreters in Rust
 
Developing iOS apps with Swift
Developing iOS apps with SwiftDeveloping iOS apps with Swift
Developing iOS apps with Swift
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
 
Stupid Awesome Python Tricks
Stupid Awesome Python TricksStupid Awesome Python Tricks
Stupid Awesome Python Tricks
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to Swift
 
Modern JS with ES6
Modern JS with ES6Modern JS with ES6
Modern JS with ES6
 
Rust言語紹介
Rust言語紹介Rust言語紹介
Rust言語紹介
 
A Few Interesting Things in Apple's Swift Programming Language
A Few Interesting Things in Apple's Swift Programming LanguageA Few Interesting Things in Apple's Swift Programming Language
A Few Interesting Things in Apple's Swift Programming Language
 
Swiftの関数型っぽい部分
Swiftの関数型っぽい部分Swiftの関数型っぽい部分
Swiftの関数型っぽい部分
 
Алексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhereАлексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhere
 
Sencha Touch Intro
Sencha Touch IntroSencha Touch Intro
Sencha Touch Intro
 
Rust concurrency tutorial 2015 12-02
Rust concurrency tutorial 2015 12-02Rust concurrency tutorial 2015 12-02
Rust concurrency tutorial 2015 12-02
 
EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is here
 

Viewers also liked

10 architectural design
10 architectural design10 architectural design
10 architectural designAyesha Bhatti
 
Chap 5 project management
Chap 5 project managementChap 5 project management
Chap 5 project managementAyesha Bhatti
 
Data structure lab 03ameer hamza
Data structure lab 03ameer hamzaData structure lab 03ameer hamza
Data structure lab 03ameer hamza
ameer hamza
 
Data Structures and Algorithms made Easy (Cover Page)
Data Structures and Algorithms made Easy (Cover Page)Data Structures and Algorithms made Easy (Cover Page)
Data Structures and Algorithms made Easy (Cover Page)
CareerMonk Publications
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
Deyvessh kumar
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manualSANTOSH RATH
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Make Mannan
 
Data structures lab c programs
Data structures lab  c programsData structures lab  c programs
Data structures lab c programs
Syed Mustafa
 
Data structures and algorithms made easy
Data structures and algorithms made easyData structures and algorithms made easy
Data structures and algorithms made easy
CareerMonk Publications
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Saket Pathak
 
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
vtunotesbysree
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notesSrikanth
 
Lab manual of C++
Lab manual of C++Lab manual of C++
Lab manual of C++
thesaqib
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
Syed Mustafa
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its typesNavtar Sidhu Brar
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
Free Open Source Software Technology Lab
 

Viewers also liked (20)

10 architectural design
10 architectural design10 architectural design
10 architectural design
 
Chap 5 project management
Chap 5 project managementChap 5 project management
Chap 5 project management
 
Data structure lab 03ameer hamza
Data structure lab 03ameer hamzaData structure lab 03ameer hamza
Data structure lab 03ameer hamza
 
Data Structures and Algorithms made Easy (Cover Page)
Data Structures and Algorithms made Easy (Cover Page)Data Structures and Algorithms made Easy (Cover Page)
Data Structures and Algorithms made Easy (Cover Page)
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 
C program
C programC program
C program
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
 
Data structures lab c programs
Data structures lab  c programsData structures lab  c programs
Data structures lab c programs
 
Data structures and algorithms made easy
Data structures and algorithms made easyData structures and algorithms made easy
Data structures and algorithms made easy
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notes
 
Lab manual of C++
Lab manual of C++Lab manual of C++
Lab manual of C++
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Data structures
Data structuresData structures
Data structures
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 

Similar to Assignment

I need to find run time analysis and description of the algo.pdf
I need to find run time analysis and description of the algo.pdfI need to find run time analysis and description of the algo.pdf
I need to find run time analysis and description of the algo.pdf
aakashenterprises
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
arri2009av
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdf
shreeaadithyaacellso
 
Game unleashedjavascript
Game unleashedjavascriptGame unleashedjavascript
Game unleashedjavascript
Reece Carlson
 
I am asked to provide the testing cases for the following co.pdf
I am asked to provide the testing cases for the following co.pdfI am asked to provide the testing cases for the following co.pdf
I am asked to provide the testing cases for the following co.pdf
acecomputertcr
 
ch9_additional.ppt
ch9_additional.pptch9_additional.ppt
ch9_additional.ppt
LokeshK66
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdfg++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
arakalamkah11
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
Russell Childs
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
piyush Kumar Sharma
 
連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」
matuura_core
 
Modern C++ Concurrency API
Modern C++ Concurrency APIModern C++ Concurrency API
Modern C++ Concurrency API
Seok-joon Yun
 
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfPart 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
mohammadirfan136964
 
Chapter 2
Chapter 2Chapter 2
C++ L04-Array+String
C++ L04-Array+StringC++ L04-Array+String
C++ L04-Array+String
Mohammad Shaker
 
The purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfThe purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdf
Rahul04August
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structureSaad Gabr
 

Similar to Assignment (20)

I need to find run time analysis and description of the algo.pdf
I need to find run time analysis and description of the algo.pdfI need to find run time analysis and description of the algo.pdf
I need to find run time analysis and description of the algo.pdf
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdf
 
Game unleashedjavascript
Game unleashedjavascriptGame unleashedjavascript
Game unleashedjavascript
 
I am asked to provide the testing cases for the following co.pdf
I am asked to provide the testing cases for the following co.pdfI am asked to provide the testing cases for the following co.pdf
I am asked to provide the testing cases for the following co.pdf
 
ch9_additional.ppt
ch9_additional.pptch9_additional.ppt
ch9_additional.ppt
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdfg++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」
 
Modern C++ Concurrency API
Modern C++ Concurrency APIModern C++ Concurrency API
Modern C++ Concurrency API
 
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfPart 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
C++ L04-Array+String
C++ L04-Array+StringC++ L04-Array+String
C++ L04-Array+String
 
Sbaw090908
Sbaw090908Sbaw090908
Sbaw090908
 
ES6 and BEYOND
ES6 and BEYONDES6 and BEYOND
ES6 and BEYOND
 
The purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfThe purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdf
 
Lec2&3_DataStructure
Lec2&3_DataStructureLec2&3_DataStructure
Lec2&3_DataStructure
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 

More from Ayesha Bhatti (18)

Aibo
AiboAibo
Aibo
 
A2
A2A2
A2
 
11 topic 9 ooa
11 topic 9 ooa11 topic 9 ooa
11 topic 9 ooa
 
10 architectural design (1)
10 architectural design (1)10 architectural design (1)
10 architectural design (1)
 
8 system models
8 system models8 system models
8 system models
 
8 system models (1)
8 system models (1)8 system models (1)
8 system models (1)
 
8 bop
8 bop8 bop
8 bop
 
8 bop (1)
8 bop (1)8 bop (1)
8 bop (1)
 
7 foreign policy process
7 foreign policy process7 foreign policy process
7 foreign policy process
 
7 foreign policy process (1)
7 foreign policy process (1)7 foreign policy process (1)
7 foreign policy process (1)
 
5 cold war era 1
5 cold war era 15 cold war era 1
5 cold war era 1
 
3 national power
3 national power3 national power
3 national power
 
1 introduction
1 introduction1 introduction
1 introduction
 
1 introduction (1)
1 introduction (1)1 introduction (1)
1 introduction (1)
 
A1
A1A1
A1
 
Project final
Project finalProject final
Project final
 
Assignment 3
Assignment 3Assignment 3
Assignment 3
 
Aibo
AiboAibo
Aibo
 

Assignment

  • 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 algorithms void bubbleSort(int myArray[], int size); void insertionSort(int myArray[], int size); void selectionSort(int myArray[], int size); //Utility functions void 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 same unsorted 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 other arrays 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]; }