0% found this document useful (0 votes)
3 views

c++ sorting

How sorting in c++ works

Uploaded by

aliabeed323
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

c++ sorting

How sorting in c++ works

Uploaded by

aliabeed323
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

FACULTY OF ELECTRONIC ENGINEERING TECHNOLOGY

Spring Term :2021-2022


Subject: CP II 4 th SEMESTER
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in wrong order.

Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4 (
1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap
them.
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one
whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Following is C ++ implementation of Bubble Sort.
#include<iostream>
using namespace std;
void bubbleSort(int []);
int main()
{
int i, arr[7];
cout<<"Enter 7 Elements: ";
for(i=0; i<7; i++)
cin>>arr[i];
bubbleSort(arr);
cout<<"\nThe New Sorted Array is: \n";
for(i=0; i<7; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
void bubbleSort(int arr[])
{
int i, j, temp;
Lecturer Alhadi . A Khaleefa 1
FACULTY OF ELECTRONIC ENGINEERING TECHNOLOGY
Spring Term :2021-2022
Subject: CP II 4 th SEMESTER
for(i=0; i<7; i++)
{
for(j=0; j<(7-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}}

Selection sort
Selection sort algorithm, we rearrange the list by selecting an element in the list and moving it to its
proper position. This algorithm finds the location of the smallest element in the unsorted portion of
the list and moves it to the top of the unsorted portion of the list. The first time, we locate the smallest
item in the entire list. The second time, we locate the smallest item in the list starting from the second
element in the list, and so on.

Here complete c++ program selection sort


//complete code for Selection sort

#include <iostream >


using namespace std;
void selectionSort(int *array,int length) //selection sort function

Lecturer Alhadi . A Khaleefa 2


FACULTY OF ELECTRONIC ENGINEERING TECHNOLOGY
Spring Term :2021-2022
Subject: CP II 4 th SEMESTER
{
int i,j,min,minat;
for(i=0;i<(length-1);i++)
{
minat=i;
min=array[i];

for(j=i+1;j<(length);j++) //select the min of the rest of array


{
if(min>array[j]) //ascending order for descending reverse
{
minat=j; //the position of the min element
min=array[j];
}
}
int temp=array[i] ;
array[i]=array[minat]; //swap
array[minat]=temp;
}
}

void printElements(int *array,int length) //print array elements


{
int i=0;
for(i=0;i<10;i++)
cout<<array[i]<<" ";
cout<<endl;
}
main()
{int a[]={9,6,5,23,2,6,2,-7,1,8}; // unsorted array
cout<<" Array before sorting"<<endl;
for(int i=0;i<10;i++)
cout<<a[i]<<" ";

Lecturer Alhadi . A Khaleefa 3


FACULTY OF ELECTRONIC ENGINEERING TECHNOLOGY
Spring Term :2021-2022
Subject: CP II 4 th SEMESTER

cout<<endl;
cout<<"array after sorting"<<endl;
selectionSort(a,10); //call to selection sort
printElements(a,10); // print elements

return 0;
}

Insertion Sort
Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our
hands.

Algorithm
// Sort an arr[] of size n
insertionSort(arr, n)
Loop from i = 1 to n-1.
a) Pick element arr[i] and insert it into sorted sequence arr[0…i-1]
Example:
12, 11, 13, 5, 6
Let us loop for i = 1 (second element of the array) to 5 (Size of input array)
i = 1. Since 11 is smaller than 12, move 12 and insert 11 before 12
11, 12, 13, 5, 6
i = 2. 13 will remain at its position as all elements in A[0..I-1] are smaller than 13
11, 12, 13, 5, 6
i = 3. 5 will move to the beginning and all other elements from 11 to 13 will move one position ahead of
their current position.
5, 11, 12, 13, 6

Lecturer Alhadi . A Khaleefa 4


FACULTY OF ELECTRONIC ENGINEERING TECHNOLOGY
Spring Term :2021-2022
Subject: CP II 4 th SEMESTER

i = 4. 6 will move to position after 5, and elements from 11 to 13 will move one position ahead of their
current position.
5, 6, 11, 12, 13
Example write c++ program to sort the following array []={
#include <iostream>
#include <stdlib.h>
using namespace std;
// C ++ program for insertion sort
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;

/* Move elements of arr[0..i-1], that are greater than


key, to one position ahead of their current
position */
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
// A utility function to print an array of size n void
printArray(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
cout<< arr[i]<<"\t";
cout<<"\n";
}

/* Driver program to test insertion sort */


int main()
{
int arr[] = {32, 11, 13, 55, 6};
int n = 5;
cout<<"Display Array before sorting ";
printArray(arr, n); //calling function printarray display array
cout<<"Display Array After sorting using insertion sort";
insertionSort(arr, n); //functioncalling function insertsort
cout<<”\t”;

Lecturer Alhadi . A Khaleefa 5


FACULTY OF ELECTRONIC ENGINEERING TECHNOLOGY
Spring Term :2021-2022
Subject: CP II 4 th SEMESTER
printArray(arr, n);
system("PAUSE");
return 0;
}

When execute the above program result will be as shown


Display array before insertion sorting 12 11 13 5 6
Display array after insertion sorting 5 6 11 12 1

Assignment
Q1 write program to sort the array myarray[]={26,7,8,55,4,3,70}
1-using selection sort.
2- using insertion sort.
Q2 write program to sort the array k[]={9,4,6,7,3,1,10,2,11,5} using bubble sort?

Lecturer Alhadi . A Khaleefa 6

You might also like