c++ sorting
c++ sorting
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.
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
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;
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?