SlideShare a Scribd company logo
1 of 12
Download to read offline
http://www.tutorialspoint.com/data_structures_algorithms/bubble_sort_algorithm.htm Copyright Β© tutorialspoint.com
DATA STRUCTURE - BUBBLE SORT ALGORITHMDATA STRUCTURE - BUBBLE SORT ALGORITHM
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison based algorithm in
which each pair of adjacent elements is compared and elements are swapped if they are not in
order. This algorithm is not suitable for large data sets as its average and worst case complexity
are of O(n2) where n are no. of items.
How bubble sort works?
We take an unsorted array for our example. Bubble sort take Ο(n2) time so we're keeping short
and precise.
Bubble sort starts with very first two elements, comparing them to check which one is greater.
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33
with 27.
We find that 27 is smaller than 33 and these two values must be swapped.
The new array should look like this βˆ’
Next we compare 33 and 35. We find that both are in already sorted positions.
Then we move to next two values, 35 and 10.
We know than 10 is smaller 35. Hence they are not sorted.
We swap these values. We find that we reach at the end of the array. After one iteration the array
should look like this βˆ’
To be precise, we are now showing that how array should look like after each iteration. After
second iteration, it should look like this βˆ’
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that array is completely sorted.
Now we should look into some practical aspects of bubble sort.
Algorithm
We assume list is an array of n elements. We further assume that swap function, swaps the values
of given array elements.
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
Pseudocode
We observe in algorithm that Bubble Sort compares each pair of array element unless the whole
array is completely sorted ascending. This may cause few complexity issues like what if the array
needs no more swapping as all the elements are already ascending.
To ease-out the issue, we use one flag variable swapped which will help us to see if any swap is
happened or not. If no swap is occurred, i.e. the array requires no more processing to be sorted, it
will come out of the loop.
Pseudocode of BubbleSort algorithm can be written as given below βˆ’
procedure bubbleSort( list : array of items )
loop = list.count;
for i = 0 to loop-1 do:
swapped = false
for j = 0 to loop-1 do:
/* compare the adjacent elements */
if list[j] > list[j+1] then
/* swap them */
swap( list[j], list[j+1] )
swapped = true
end if
end for
/*if no number was swapped that means
array is sorted now, break the loop.*/
if(not swapped) then
break
end if
end for
end procedure return list
Implementation
One more issue we did not address in our original algorithm and its improvised pseudocode, that
is, after every iteration the highest values settles down at the end of the array. So next iteration
needs not to include already sorted elements. For this purpose, in our implementation, we restrict
the inner loop to avoid already sorted values.
To see bubble sort implementation in C programming language, please click here.
bubble.txt
/* Bubble sort code */
#include <iostream.h>
int main()
{
int array[100], n, c, d, swap;
cout<<"Enter number of elementsn";
cin>>n;
cout<<"Enter"<<n<<"integersn";
for (c = 0; c < n; c++)
cin>>array[c];
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
cout<<"Sorted list in ascending order:n";
for ( c = 0 ; c < n ; c++ )
cout<<"n"<< array[c];
return 0;
}
Page 1
http://www.tutorialspoint.com/data_structures_algorithms/insertion_sort_algorithm.htm
Copyright Β© tutorialspoint.com
DATA STRUCTURE - INSERTION SORTDATA STRUCTURE - INSERTION SORT
This is a in-place comparison based sorting algorithm. Here, a sub-list is maintained which is
always sorted. For example, the lower part of an array is maintained to be sorted. A element which
is to be 'insert'ed in this sorted sub-list, has to find its appropriate place and insert it there. Hence
the name insertion sort.
The array is searched sequentially and unsorted items are moved and inserted into sorted sub-list
inthesamearray. This algorithm is not suitable for large data sets as its average and worst case
complexity are of Ο(n2) where n are no. of items.
How insertion sort works?
We take an unsorted array for our example.
Insertion sort compares the first two elements.
It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.
Insertion sort moves ahead and compares 33 with 27.
And finds that 33 is not in correct position.
It swaps 33 with 27. Also it checks with all the elements of sorted sublist. Here we see that sorted
sub-list has only one element 14 and 27 is greater than 14. Hence sorted sub-list remain sorted
after swapping.
By now we have 14 and 27 in the sorted sublist. Next it compares 33 with 10,.
These values are not in sorted order.
So we swap them.
But swapping makes 27 and 10 unsorted.
So we swap them too.
Again we find 14 and 10 in unsorted order.
And we swap them. By the end of third iteration we have a sorted sublist of 3 items.
This process goes until all the unsorted values are covered in sorted sublist. And now we shall see
some programming aspects of insertion sort.
Algorithm
Now we have a bigger picture of how this sorting technique works, so we can derive simple steps
by which we can achieve insertion sort.
Step 1 βˆ’ If it is the first element, it is already sorted. return 1;
Step 1 βˆ’ Pick next element
Step 1 βˆ’ Compare with all elements in the sorted sub-list
Step 1 βˆ’ Find appropriate position
Step 1 βˆ’ Insert the value
Step 1 βˆ’ Repeat until list is sorted
Pseudocode
procedure insertionSort( A : array of items )
int holePosition
int valueToInsert
for i = 1 to length(A) inclusive do:
/* select value to be inserted */
valueToInsert = A[i]
holePosition = i
/*locate hole position for the element to be inserted */
while holePosition > 0 and A[i-1] > valueToInsert do:
A[holePosition] = A[holePosition-1]
holePosition = holePosition -1
end while
/* insert the number at hole position */
A[holePosition] = valueToInsert
end for
end procedure
To see insertion sort implementation in C programming language, please click here.
Loading [MathJax]/jax/output/HTML-CSS/jax.js
insertion.txt
/* insertion sort code */
#include <iostream.h>
int main()
{
int array[100], n, c, d, swap;
cout<<"Enter number of elementsn";
cin>>n;
cout<<"Enter"<<n<<"integersn";
for (c = 0; c < n; c++)
cin>>array[c];
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d] < array[d-1]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
cout<<"Sorted list in ascending order:n";
for ( c = 0 ; c < n ; c++ )
cout<<"n"<< array[c];
return 0;
}
Page 1
http://www.tutorialspoint.com/data_structures_algorithms/selection_sort_algorithm.htm
Copyright Β© tutorialspoint.com
DATA STRUCTURE - SELECTION SORTDATA STRUCTURE - SELECTION SORT
Selection sort is a simple sorting algorithm. This sorting algorithm is a in-place comparison based
algorithm in which the list is divided into two parts, sorted part at left end and unsorted part at right
end. Initially sorted part is empty and unsorted part is entire list.
Smallest element is selected from the unsorted array and swapped with the leftmost element and
that element becomes part of sorted array. This process continues moving unsorted array
boundary by one element to the right.
This algorithm is not suitable for large data sets as its average and worst case complexity are of
O(n2) where n are no. of items.
How selection sort works?
We take the below depicted array for our example.
For the first position in the sorted list, the whole list is scanned sequentially. The first position
where 14 is stored presently, we search the whole list and find that 10 is the lowest value.
So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the
list, appears in the first position of sorted list.
For the second position, where 33 is residing, we start scanning the rest of the list in linear manner.
We find that 14 is the second lowest value in the list and it should appear at the second place. We
swap these values.
After two iterations, two least values are positioned at the the beginning in the sorted manner.
The same process is applied on the rest of the items in the array. We shall see an pictorial
depiction of entire sorting process βˆ’
Now, we should learn some programming aspects of selection sort.
Algorithm
Step 1 βˆ’ Set MIN to location 0
Step 2 βˆ’ Search the minimum element in the list
Step 3 βˆ’ Swap with value at location MIN
Step 4 βˆ’ Increment MIN to point to next element
Step 5 βˆ’ Repeat until list is sorted
Pseudocode
procedure selection sort
list : array of items
n : size of list
for i = 1 to n - 1
/* set current element as minimum*/
min = i
/* check the element to be minimum */
for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for
/* swap the minimum element with the current element*/
if indexMin != i then
swap list[min] and list[i]
end if
end for
end procedure
To see selection sort implementation in C programming language, please click here.
selection.txt
/* selection sort code */
#include <iostream.h>
int main()
{
int array[100], n, c, d, swap;
cout<<"Enter number of elementsn";
cin>>n;
cout<<"Enter"<<n<<"integersn";
for (c = 0; c < n; c++)
cin>>array[c];
for ( c = 0 ; c < ( n - 1 ) ; c++ )
{
position = c;
for ( d = c + 1 ; d < n ; d++ )
{
if ( array[position] > array[d] )
position = d;
}
if ( position != c )
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
cout<<"Sorted list in ascending order:n";
for ( c = 0 ; c < n ; c++ )
cout<<"n"<< array[c];
return 0;
}
Page 1

More Related Content

What's hot

IOT System Management with NETCONF-YANG.pptx
IOT System Management with NETCONF-YANG.pptxIOT System Management with NETCONF-YANG.pptx
IOT System Management with NETCONF-YANG.pptxArchanaPandiyan
Β 
Embedded systems
Embedded systemsEmbedded systems
Embedded systemsManju Nathan
Β 
Design and Implementation of Hospital Management System Using Java
Design and Implementation of Hospital Management System Using JavaDesign and Implementation of Hospital Management System Using Java
Design and Implementation of Hospital Management System Using JavaIOSR Journals
Β 
Online Leave Management Project
Online Leave Management ProjectOnline Leave Management Project
Online Leave Management Projectanuj pathak
Β 
Actuators and optical speed sensor and application in automatic transmission
Actuators and optical speed sensor and application in automatic transmissionActuators and optical speed sensor and application in automatic transmission
Actuators and optical speed sensor and application in automatic transmissionZIYAD AMBALANGADAN
Β 
Blood Bank Management System
Blood Bank Management SystemBlood Bank Management System
Blood Bank Management SystemKavi
Β 
introduction to scientific computing
introduction to scientific computingintroduction to scientific computing
introduction to scientific computingHaiderParekh1
Β 
Erd chapter 3
Erd chapter 3Erd chapter 3
Erd chapter 3Nargis Ehsan
Β 
Entity (types, attibute types)
Entity (types, attibute types)Entity (types, attibute types)
Entity (types, attibute types)Zaheer Soomro
Β 
Fieldbus technology industrial automation
Fieldbus technology industrial automationFieldbus technology industrial automation
Fieldbus technology industrial automationPradeep Dhakad
Β 
Industrial Control Systems - PLC
Industrial Control Systems - PLCIndustrial Control Systems - PLC
Industrial Control Systems - PLCBehzad Samadi
Β 
REST APIs for the Internet of Things
REST APIs for the Internet of ThingsREST APIs for the Internet of Things
REST APIs for the Internet of ThingsMichael Koster
Β 
Database normalization
Database normalizationDatabase normalization
Database normalizationVaibhav Kathuria
Β 
Introduction to Embedded System
Introduction to Embedded SystemIntroduction to Embedded System
Introduction to Embedded SystemZakaria Gomaa
Β 
SRS on Online Blood Bank Managment system...
SRS on Online Blood Bank Managment system... SRS on Online Blood Bank Managment system...
SRS on Online Blood Bank Managment system... GCWUF
Β 
HOPFIELD NETWORK
HOPFIELD NETWORKHOPFIELD NETWORK
HOPFIELD NETWORKankita pandey
Β 
Doctor appointment system.docx
Doctor appointment system.docxDoctor appointment system.docx
Doctor appointment system.docxbbc53020
Β 
Uml diagram for_hospital_management_system
Uml diagram for_hospital_management_systemUml diagram for_hospital_management_system
Uml diagram for_hospital_management_systemPradeep Bhosale
Β 

What's hot (20)

Exhaustive Search
Exhaustive SearchExhaustive Search
Exhaustive Search
Β 
IOT System Management with NETCONF-YANG.pptx
IOT System Management with NETCONF-YANG.pptxIOT System Management with NETCONF-YANG.pptx
IOT System Management with NETCONF-YANG.pptx
Β 
Embedded systems
Embedded systemsEmbedded systems
Embedded systems
Β 
Design and Implementation of Hospital Management System Using Java
Design and Implementation of Hospital Management System Using JavaDesign and Implementation of Hospital Management System Using Java
Design and Implementation of Hospital Management System Using Java
Β 
Online Leave Management Project
Online Leave Management ProjectOnline Leave Management Project
Online Leave Management Project
Β 
Actuators and optical speed sensor and application in automatic transmission
Actuators and optical speed sensor and application in automatic transmissionActuators and optical speed sensor and application in automatic transmission
Actuators and optical speed sensor and application in automatic transmission
Β 
Blood Bank Management System
Blood Bank Management SystemBlood Bank Management System
Blood Bank Management System
Β 
introduction to scientific computing
introduction to scientific computingintroduction to scientific computing
introduction to scientific computing
Β 
Erd chapter 3
Erd chapter 3Erd chapter 3
Erd chapter 3
Β 
Entity (types, attibute types)
Entity (types, attibute types)Entity (types, attibute types)
Entity (types, attibute types)
Β 
Fieldbus technology industrial automation
Fieldbus technology industrial automationFieldbus technology industrial automation
Fieldbus technology industrial automation
Β 
Industrial Control Systems - PLC
Industrial Control Systems - PLCIndustrial Control Systems - PLC
Industrial Control Systems - PLC
Β 
REST APIs for the Internet of Things
REST APIs for the Internet of ThingsREST APIs for the Internet of Things
REST APIs for the Internet of Things
Β 
Database normalization
Database normalizationDatabase normalization
Database normalization
Β 
Int306 02
Int306 02Int306 02
Int306 02
Β 
Introduction to Embedded System
Introduction to Embedded SystemIntroduction to Embedded System
Introduction to Embedded System
Β 
SRS on Online Blood Bank Managment system...
SRS on Online Blood Bank Managment system... SRS on Online Blood Bank Managment system...
SRS on Online Blood Bank Managment system...
Β 
HOPFIELD NETWORK
HOPFIELD NETWORKHOPFIELD NETWORK
HOPFIELD NETWORK
Β 
Doctor appointment system.docx
Doctor appointment system.docxDoctor appointment system.docx
Doctor appointment system.docx
Β 
Uml diagram for_hospital_management_system
Uml diagram for_hospital_management_systemUml diagram for_hospital_management_system
Uml diagram for_hospital_management_system
Β 

Viewers also liked

Bubblesort Algorithm
Bubblesort AlgorithmBubblesort Algorithm
Bubblesort AlgorithmTobias Straub
Β 
Bubble sort a best presentation topic
Bubble sort a best presentation topicBubble sort a best presentation topic
Bubble sort a best presentation topicSaddam Hussain
Β 
Bubble Sort
Bubble SortBubble Sort
Bubble Sortgeeortiz
Β 
Bubble sort
Bubble sortBubble sort
Bubble sortManek Ar
Β 
Intersection Study - Algorithm(Sort)
Intersection Study - Algorithm(Sort)Intersection Study - Algorithm(Sort)
Intersection Study - Algorithm(Sort)Jea Hyeun Jung
Β 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sortKrish_ver2
Β 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
Β 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
Β 
Chemistry - Chp 4 - Atomic Structure - PowerPoint
Chemistry - Chp 4 - Atomic Structure - PowerPointChemistry - Chp 4 - Atomic Structure - PowerPoint
Chemistry - Chp 4 - Atomic Structure - PowerPointMr. Walajtys
Β 
time-management-ppt
time-management-ppttime-management-ppt
time-management-pptAgrima
Β 

Viewers also liked (15)

Bubblesort Algorithm
Bubblesort AlgorithmBubblesort Algorithm
Bubblesort Algorithm
Β 
Bubble sort a best presentation topic
Bubble sort a best presentation topicBubble sort a best presentation topic
Bubble sort a best presentation topic
Β 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
Β 
Bubble sort
Bubble sortBubble sort
Bubble sort
Β 
Python Day1
Python Day1Python Day1
Python Day1
Β 
Bubble sort
Bubble sortBubble sort
Bubble sort
Β 
Intersection Study - Algorithm(Sort)
Intersection Study - Algorithm(Sort)Intersection Study - Algorithm(Sort)
Intersection Study - Algorithm(Sort)
Β 
Bubble sort algorithm
Bubble sort algorithmBubble sort algorithm
Bubble sort algorithm
Β 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sort
Β 
Sorting
SortingSorting
Sorting
Β 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
Β 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
Β 
String
StringString
String
Β 
Chemistry - Chp 4 - Atomic Structure - PowerPoint
Chemistry - Chp 4 - Atomic Structure - PowerPointChemistry - Chp 4 - Atomic Structure - PowerPoint
Chemistry - Chp 4 - Atomic Structure - PowerPoint
Β 
time-management-ppt
time-management-ppttime-management-ppt
time-management-ppt
Β 

Similar to Sorting

Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
Β 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
Β 
Selection sort
Selection sortSelection sort
Selection sortasra khan
Β 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithmsCHANDAN KUMAR
Β 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
Β 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sortingsajinis3
Β 
Merge radix-sort-algorithm
Merge radix-sort-algorithmMerge radix-sort-algorithm
Merge radix-sort-algorithmRendell Inocencio
Β 
Merge radix-sort-algorithm
Merge radix-sort-algorithmMerge radix-sort-algorithm
Merge radix-sort-algorithmRendell Inocencio
Β 
Bubble sorting lab manual
Bubble sorting lab manualBubble sorting lab manual
Bubble sorting lab manualmaamir farooq
Β 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
Β 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...Tosin Amuda
Β 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
Β 

Similar to Sorting (20)

Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
Β 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
Β 
Sorting
SortingSorting
Sorting
Β 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
Β 
Selection sort
Selection sortSelection sort
Selection sort
Β 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
Β 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Β 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
Β 
my docoment
my docomentmy docoment
my docoment
Β 
Sorting
SortingSorting
Sorting
Β 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
Β 
Merge radix-sort-algorithm
Merge radix-sort-algorithmMerge radix-sort-algorithm
Merge radix-sort-algorithm
Β 
Merge radix-sort-algorithm
Merge radix-sort-algorithmMerge radix-sort-algorithm
Merge radix-sort-algorithm
Β 
Bubble sorting lab manual
Bubble sorting lab manualBubble sorting lab manual
Bubble sorting lab manual
Β 
Unit v data structure-converted
Unit  v data structure-convertedUnit  v data structure-converted
Unit v data structure-converted
Β 
Unit6 C
Unit6 C Unit6 C
Unit6 C
Β 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
Β 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
Β 
Ada notes
Ada notesAda notes
Ada notes
Β 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Β 

Recently uploaded

PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
Β 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
Β 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfIdiosysTechnologies1
Β 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
Β 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
Β 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
Β 
Dealing with Cultural Dispersion β€” Stefano Lambiase β€” ICSE-SEIS 2024
Dealing with Cultural Dispersion β€” Stefano Lambiase β€” ICSE-SEIS 2024Dealing with Cultural Dispersion β€” Stefano Lambiase β€” ICSE-SEIS 2024
Dealing with Cultural Dispersion β€” Stefano Lambiase β€” ICSE-SEIS 2024StefanoLambiase
Β 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
Β 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
Β 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
Β 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
Β 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
Β 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
Β 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
Β 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
Β 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
Β 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
Β 
εŠžη†ε­¦δ½θ―(UQ文凭证书)ζ˜†ε£«ε…°ε€§ε­¦ζ―•δΈšθ―ζˆη»©ε•εŽŸη‰ˆδΈ€ζ¨‘δΈ€ζ ·
εŠžη†ε­¦δ½θ―(UQ文凭证书)ζ˜†ε£«ε…°ε€§ε­¦ζ―•δΈšθ―ζˆη»©ε•εŽŸη‰ˆδΈ€ζ¨‘δΈ€ζ ·εŠžη†ε­¦δ½θ―(UQ文凭证书)ζ˜†ε£«ε…°ε€§ε­¦ζ―•δΈšθ―ζˆη»©ε•εŽŸη‰ˆδΈ€ζ¨‘δΈ€ζ ·
εŠžη†ε­¦δ½θ―(UQ文凭证书)ζ˜†ε£«ε…°ε€§ε­¦ζ―•δΈšθ―ζˆη»©ε•εŽŸη‰ˆδΈ€ζ¨‘δΈ€ζ ·umasea
Β 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
Β 

Recently uploaded (20)

PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
Β 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
Β 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdf
Β 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
Β 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Β 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
Β 
Dealing with Cultural Dispersion β€” Stefano Lambiase β€” ICSE-SEIS 2024
Dealing with Cultural Dispersion β€” Stefano Lambiase β€” ICSE-SEIS 2024Dealing with Cultural Dispersion β€” Stefano Lambiase β€” ICSE-SEIS 2024
Dealing with Cultural Dispersion β€” Stefano Lambiase β€” ICSE-SEIS 2024
Β 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
Β 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Β 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
Β 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Β 
Hot Sexy call girls in Patel NagarπŸ” 9953056974 πŸ” escort Service
Hot Sexy call girls in Patel NagarπŸ” 9953056974 πŸ” escort ServiceHot Sexy call girls in Patel NagarπŸ” 9953056974 πŸ” escort Service
Hot Sexy call girls in Patel NagarπŸ” 9953056974 πŸ” escort Service
Β 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
Β 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
Β 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
Β 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
Β 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Β 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
Β 
εŠžη†ε­¦δ½θ―(UQ文凭证书)ζ˜†ε£«ε…°ε€§ε­¦ζ―•δΈšθ―ζˆη»©ε•εŽŸη‰ˆδΈ€ζ¨‘δΈ€ζ ·
εŠžη†ε­¦δ½θ―(UQ文凭证书)ζ˜†ε£«ε…°ε€§ε­¦ζ―•δΈšθ―ζˆη»©ε•εŽŸη‰ˆδΈ€ζ¨‘δΈ€ζ ·εŠžη†ε­¦δ½θ―(UQ文凭证书)ζ˜†ε£«ε…°ε€§ε­¦ζ―•δΈšθ―ζˆη»©ε•εŽŸη‰ˆδΈ€ζ¨‘δΈ€ζ ·
εŠžη†ε­¦δ½θ―(UQ文凭证书)ζ˜†ε£«ε…°ε€§ε­¦ζ―•δΈšθ―ζˆη»©ε•εŽŸη‰ˆδΈ€ζ¨‘δΈ€ζ ·
Β 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
Β 

Sorting

  • 1. http://www.tutorialspoint.com/data_structures_algorithms/bubble_sort_algorithm.htm Copyright Β© tutorialspoint.com DATA STRUCTURE - BUBBLE SORT ALGORITHMDATA STRUCTURE - BUBBLE SORT ALGORITHM Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison based algorithm in which each pair of adjacent elements is compared and elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average and worst case complexity are of O(n2) where n are no. of items. How bubble sort works? We take an unsorted array for our example. Bubble sort take Ο(n2) time so we're keeping short and precise. Bubble sort starts with very first two elements, comparing them to check which one is greater. In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with 27. We find that 27 is smaller than 33 and these two values must be swapped. The new array should look like this βˆ’ Next we compare 33 and 35. We find that both are in already sorted positions. Then we move to next two values, 35 and 10. We know than 10 is smaller 35. Hence they are not sorted.
  • 2. We swap these values. We find that we reach at the end of the array. After one iteration the array should look like this βˆ’ To be precise, we are now showing that how array should look like after each iteration. After second iteration, it should look like this βˆ’ Notice that after each iteration, at least one value moves at the end. And when there's no swap required, bubble sorts learns that array is completely sorted. Now we should look into some practical aspects of bubble sort. Algorithm We assume list is an array of n elements. We further assume that swap function, swaps the values of given array elements. begin BubbleSort(list) for all elements of list if list[i] > list[i+1] swap(list[i], list[i+1]) end if end for return list end BubbleSort Pseudocode We observe in algorithm that Bubble Sort compares each pair of array element unless the whole array is completely sorted ascending. This may cause few complexity issues like what if the array needs no more swapping as all the elements are already ascending. To ease-out the issue, we use one flag variable swapped which will help us to see if any swap is happened or not. If no swap is occurred, i.e. the array requires no more processing to be sorted, it will come out of the loop. Pseudocode of BubbleSort algorithm can be written as given below βˆ’ procedure bubbleSort( list : array of items ) loop = list.count; for i = 0 to loop-1 do: swapped = false for j = 0 to loop-1 do:
  • 3. /* compare the adjacent elements */ if list[j] > list[j+1] then /* swap them */ swap( list[j], list[j+1] ) swapped = true end if end for /*if no number was swapped that means array is sorted now, break the loop.*/ if(not swapped) then break end if end for end procedure return list Implementation One more issue we did not address in our original algorithm and its improvised pseudocode, that is, after every iteration the highest values settles down at the end of the array. So next iteration needs not to include already sorted elements. For this purpose, in our implementation, we restrict the inner loop to avoid already sorted values. To see bubble sort implementation in C programming language, please click here.
  • 4. bubble.txt /* Bubble sort code */ #include <iostream.h> int main() { int array[100], n, c, d, swap; cout<<"Enter number of elementsn"; cin>>n; cout<<"Enter"<<n<<"integersn"; for (c = 0; c < n; c++) cin>>array[c]; for (c = 0 ; c < ( n - 1 ); c++) { for (d = 0 ; d < n - c - 1; d++) { if (array[d] > array[d+1]) /* For decreasing order use < */ { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; } } } cout<<"Sorted list in ascending order:n"; for ( c = 0 ; c < n ; c++ ) cout<<"n"<< array[c]; return 0; } Page 1
  • 5. http://www.tutorialspoint.com/data_structures_algorithms/insertion_sort_algorithm.htm Copyright Β© tutorialspoint.com DATA STRUCTURE - INSERTION SORTDATA STRUCTURE - INSERTION SORT This is a in-place comparison based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. A element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate place and insert it there. Hence the name insertion sort. The array is searched sequentially and unsorted items are moved and inserted into sorted sub-list inthesamearray. This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where n are no. of items. How insertion sort works? We take an unsorted array for our example. Insertion sort compares the first two elements. It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list. Insertion sort moves ahead and compares 33 with 27. And finds that 33 is not in correct position. It swaps 33 with 27. Also it checks with all the elements of sorted sublist. Here we see that sorted sub-list has only one element 14 and 27 is greater than 14. Hence sorted sub-list remain sorted after swapping. By now we have 14 and 27 in the sorted sublist. Next it compares 33 with 10,. These values are not in sorted order.
  • 6. So we swap them. But swapping makes 27 and 10 unsorted. So we swap them too. Again we find 14 and 10 in unsorted order. And we swap them. By the end of third iteration we have a sorted sublist of 3 items. This process goes until all the unsorted values are covered in sorted sublist. And now we shall see some programming aspects of insertion sort. Algorithm Now we have a bigger picture of how this sorting technique works, so we can derive simple steps by which we can achieve insertion sort. Step 1 βˆ’ If it is the first element, it is already sorted. return 1; Step 1 βˆ’ Pick next element Step 1 βˆ’ Compare with all elements in the sorted sub-list Step 1 βˆ’ Find appropriate position Step 1 βˆ’ Insert the value Step 1 βˆ’ Repeat until list is sorted Pseudocode procedure insertionSort( A : array of items ) int holePosition int valueToInsert for i = 1 to length(A) inclusive do: /* select value to be inserted */ valueToInsert = A[i] holePosition = i /*locate hole position for the element to be inserted */ while holePosition > 0 and A[i-1] > valueToInsert do:
  • 7. A[holePosition] = A[holePosition-1] holePosition = holePosition -1 end while /* insert the number at hole position */ A[holePosition] = valueToInsert end for end procedure To see insertion sort implementation in C programming language, please click here. Loading [MathJax]/jax/output/HTML-CSS/jax.js
  • 8. insertion.txt /* insertion sort code */ #include <iostream.h> int main() { int array[100], n, c, d, swap; cout<<"Enter number of elementsn"; cin>>n; cout<<"Enter"<<n<<"integersn"; for (c = 0; c < n; c++) cin>>array[c]; for (c = 1 ; c <= n - 1; c++) { d = c; while ( d > 0 && array[d] < array[d-1]) { t = array[d]; array[d] = array[d-1]; array[d-1] = t; d--; } } cout<<"Sorted list in ascending order:n"; for ( c = 0 ; c < n ; c++ ) cout<<"n"<< array[c]; return 0; } Page 1
  • 9. http://www.tutorialspoint.com/data_structures_algorithms/selection_sort_algorithm.htm Copyright Β© tutorialspoint.com DATA STRUCTURE - SELECTION SORTDATA STRUCTURE - SELECTION SORT Selection sort is a simple sorting algorithm. This sorting algorithm is a in-place comparison based algorithm in which the list is divided into two parts, sorted part at left end and unsorted part at right end. Initially sorted part is empty and unsorted part is entire list. Smallest element is selected from the unsorted array and swapped with the leftmost element and that element becomes part of sorted array. This process continues moving unsorted array boundary by one element to the right. This algorithm is not suitable for large data sets as its average and worst case complexity are of O(n2) where n are no. of items. How selection sort works? We take the below depicted array for our example. For the first position in the sorted list, the whole list is scanned sequentially. The first position where 14 is stored presently, we search the whole list and find that 10 is the lowest value. So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the list, appears in the first position of sorted list. For the second position, where 33 is residing, we start scanning the rest of the list in linear manner. We find that 14 is the second lowest value in the list and it should appear at the second place. We swap these values. After two iterations, two least values are positioned at the the beginning in the sorted manner. The same process is applied on the rest of the items in the array. We shall see an pictorial depiction of entire sorting process βˆ’
  • 10. Now, we should learn some programming aspects of selection sort. Algorithm Step 1 βˆ’ Set MIN to location 0 Step 2 βˆ’ Search the minimum element in the list Step 3 βˆ’ Swap with value at location MIN Step 4 βˆ’ Increment MIN to point to next element Step 5 βˆ’ Repeat until list is sorted Pseudocode procedure selection sort list : array of items n : size of list for i = 1 to n - 1 /* set current element as minimum*/ min = i /* check the element to be minimum */ for j = i+1 to n if list[j] < list[min] then min = j; end if
  • 11. end for /* swap the minimum element with the current element*/ if indexMin != i then swap list[min] and list[i] end if end for end procedure To see selection sort implementation in C programming language, please click here.
  • 12. selection.txt /* selection sort code */ #include <iostream.h> int main() { int array[100], n, c, d, swap; cout<<"Enter number of elementsn"; cin>>n; cout<<"Enter"<<n<<"integersn"; for (c = 0; c < n; c++) cin>>array[c]; for ( c = 0 ; c < ( n - 1 ) ; c++ ) { position = c; for ( d = c + 1 ; d < n ; d++ ) { if ( array[position] > array[d] ) position = d; } if ( position != c ) { swap = array[c]; array[c] = array[position]; array[position] = swap; } } cout<<"Sorted list in ascending order:n"; for ( c = 0 ; c < n ; c++ ) cout<<"n"<< array[c]; return 0; } Page 1