Bubble Sort Algorithm Explained
Bubble Sort Algorithm Explained
1 / 38
- Hence to sort a DATA[N] element bubble sort algorithm required N –
1 passes and each pass has N-PASS comparisons.
Ex.: Sort the following list DATA by using bubble sort method and shows
all passes.
42 23 74 11 65 15 10 08
Ans: In bubble sort there are n-1 passes perform i.e 8-1=7 as shown in follows.
Each pass has n-pass comparisons perform.
Circle indicate swapping of two elements
Pass 1: In pass one there are N – 1 comparison are perform as :
i. Compare DATA[1], with DATA[2], since 42 > 23, interchange
them. Hence given list becomes,
23 42 74 11 65 15 10 08
ii. Compare DATA[2] with DATA[3]. Since 42 < 74, a list not alter.
iii. Compare DATA[3] with DATA[4]. Since 74 > 11, interchange them.
Hence given list becomes,
23 42 11 74 65 15 10 08
iv. Compare DATA[4] with DATA[5]. Since 75 > 65, interchange them.
Hence given list becomes,
23 42 11 65 74 15 10 08
vi. Compare DATA[6] with DATA[7]. Since 74 > 10, interchange them.
Hence given list becomes,
23 42 11 65 15 10 74 08
2 / 38
vii. Compare DATA[7] with DATA[8]. Since 74 > 08, interchange them.
Hence given list becomes,
23 42 11 65 15 10 08 74
3 / 38
After completing of pass 3 the third largest no. 42 is bubbled up to the
position (n – 2)i.e. 6th, but the remaining number unsorted.
4 / 38
After completing of pass 5 the fifth largest no. 15 is bubbled up to
position (n – 4) i.e. 4th, but the remaining number unsorted.
Pass 6: In Pass 6, there are n-pass i.e 8-6=2 comparisons perform
in following table.
5 / 38
i. 11 10 08 15 23 42 65 74
6 / 38
ii. 10 11 08 15 23 42 65 74
10 08 11 15 23 42 65 74
08 10 11 15 23 42 65 74
After completing of pass 7, list get sorted i.e.,
08 10 11 15 23 42 65 74
8 / 38
[End of if structure]
Step 5: [Increase PTR by 1]
set PTR = PTR + 1
[End of step 3 loop]
[End of step 1 loop]
Step 6 : [Finished] Exit.
= 2 = 2 + 0(N) F(N)
= 0(N2)
- That means time require execute the bubble sort algorithm is
propositional to N2.
- Here N is the number of input elements.
9 / 38
2.1 Selection Sort Method :
- Suppose linear array list DATA is in memory with N elements
DATA[1], DATA[2], ……., DATA[N].
- A selection sort algorithm works as follow, Pass 1:
- Find the location LOC of the smallest element in the list of N elements
DATA[1], DATA[2], ……, DATA[N] and the DATA[LOC] and
DATA[1].
- Then, DATA[1] is sorted.
Pass 2:
- Find the location LOC of the smallest element form the list of N – 1
elements DATA[2], DATA[3], ……., DATA[N] and then interchange
DATA[LOC] with DATA[2].
- Then DATA[1], DATA[2] are sorted, since DATA[1] < DATA[2]1.
|
|
|
Pass N-1 :
- Find the location of the smallest element from the list of elements
DATA[N – 1], DATA[N].
- Then interchange DATA[LOC ]with DATA[N-1].
- Then DATA[1], DATA[2], ………, DATA[N] is sorted, since DATA[N –
1] <= DATA[N].
- After completing N – 1 passes the list of DATA becomes a sorted list
in ascending ordeR
10 / 38
Ex.: Sort the given list in ascending order using selection sort method
write all passes.
- Consider unsorted list DATA contains 8 elements as follows,
42 23 74 11 65 12 10 05
11 / 38
• Algorithm for Selection Sort Method :
12 / 38
[End of step 1 loop]
Step 4: [Finished] Exit.
13 / 38
Pass 2: DATA[2] is inserted either before or after DATA[1], so that
DATA[1] , DATA[2] is sorted.
Ex.: Sort the given list LA in ascending order by using insertion sort
method and shows all passes.
LA={20,50,70,55,15,10,25}
Ans: Following table shows insertion sort algorithm.
The circle element indicate Pass.
The arrow indicate proper place for element.
14 / 38
Algorithm 2.3 : INSERTIONSRT (DATA, N, PASS, PTR, TEMP)
DATA is a linear array with N unsorted element. PASS variable
is used to shows the passes. PTR is a pointer variable and
TEMP is temporary variable. This algorithm perform insertion
sort method.
Step 1: [Initialize sentinel element]
Set DATA[0] = –
Step 2: Repeat step 3 to 5
For PASS = 2 to N
Step 3: Set TEMP = DATA[PASS] and
PTR = PASS – 1
Step 4: Repeat while (TEMP < DATA[PTR])
(a) DATA[PTR + 1] = DATA[PTR]
15 / 38
[Moves element Forward]
(b) Set PTR = PTR – 1
[End of step 4 loop]
Step 5: [Insert element in proper place]
Set DATA[PTR + 1] = TEMP
[End of step 2 loop]
Step 6: Return.
= 2 = 0(N2)
- Further more, one can show that on the average their will be
approximately comparisons in the linear loop.
- Accordingly for the average case,
N 1
F(N) = + +……….. + 2
–
N(N 1)
= 4
= 0(N2)
16 / 38
- Every node of linked list is get divided into two fields.
1. First field is INFO which is used to store information of the
element.
2. Second field is LINK or next pointer field which is used to store
address of the next node in the list.
- Following figure shows a linked list with four nodes.
START
- It is clear that, in above fig. each node is pictured with two fields.
- The left field use the actual information, and right field use store the
address of the next node.
- Arrows are used to show the order of the nodes.
- The next pointer or LINK field of the last node contain a invalid
address called ‘null pointer’.
- The null pointer (X) is used to shows the end of the list.
- The linked list has also a list pointer variable called START which
contains address of first node in the list.
- The starting address o the list which is sorted in START is used to
traverse the whole linked list.
- Here if the linked list has no any node then such a list is called ‘null
list’ or ‘empty list’. And it is denoted by the null pointer assigning to
START i.e. START = NULL.
17 / 38
2.5 Representation of Linked List in Memory :-
- Suppose list be a linked list for the storage of such a list in memory it
requires linear arrays.
- One is a general array set “INFO”, another pointer array set “LINK”.
- An array INFO[K] will contain the information part and an array
LINK[k] will contain the address of the next element of the list.
- In linked list, a pointer variable START is used to store starting
location of the list. NULL is used to show the end of the list.
- If the subscripts of an array INFO & LINK will be positive integer, then
NULL = 0 used to show end of the linked list.
- Here, the nodes of the linked list doesn’t need occupy the adjacent
dement in the array INFO & LINK.
- Also more than one linked list may be maintain in the same linear
array but only by storing the static address of the second list. Another
pointer variable set START = 1 & so on.
- Ex.: suppose we want to store the words :- BCA–SEM 3 using a linked
list.
- The linked list representation of these words is as follows.
START
B C A S E M 5
18 / 38
START INFO LINK
1
2 3
3 B
0
4 E
A 6
5
6 C 8
7 – 7
8 M
5
9 5
9
2
4
START = 3 INFO[3] = B
LINK[3] = 6 INFO[6] = C
LINK[6] = 5 INFO[5] = A
LINK[5] = 7 INFO[7] = –
LINK[7] = 9 INFO[9] = S
LINK[9] = 4 INFO[4] = E
LINK[4] = 8 INFO[8] = M
LINK[8] = 2 INFO[2] = 3
LINK[2] = 0
19 / 38
- In data structure searching operation is used to find the location LOC
of an ITEM in a given list.
- As the LIST may or may not be sorted in memory.
- Hence there are two searching algorithm are used to find the location
LOC of a node where an item appear in the list.
- These algorithms are, 1. LIST is unsorted.
2. LIST is sorted.
1. LIST is Unsorted :
- An algorithm to search an ITEM in unsorted linked List.
- Let LIST is a unsorted linked list stored in memory with INFO and
LINK.
- START is a pointer variable which is used to stored initial address of
linked list.
- Then one searches for ITEM in list by traversing to the list using
pointer variable PTR and comparing ITEM with the contains
INFO[PTR] of each node, one by one of list.
20 / 38
pointer variable which indicate the node which is going to
process.
Step 1.: [Initialize pointer PTR]
set PTR START
Step 2.: Repeat step 3 while
PTR NULL
Step 3.: [Is ITEM present at node PTR?]
If ITEM = INFO[PTR], then:
Write : “Successful search” and
Set LOC= PTR and Exit.
Else
[Now point to the next node]
Set PTR = LINK[PTR]
[End of if structure]
[End of step 2 loop]
Step 4.: write “Unsuccessful search” and
Set LOC = NULL
Step 5.: [Finished] Exit.
2. LIST is Sorted :
- An algorithm to search an ITEM in sorted linked List.
- Suppose list is sorted linked list stored in memory.
- This algorithm makes search for the location LOC of an ITEM in LIST
by traversing list using pointer variable PTR.
- Here while traversing an ITEM of information is compare with each
node of the list successful.
- Here traversing is stop when, 1. ITEM is appear at location PTR.
2. ITEM < INFO[PTR] or.
3. There is no further element to make comparison i.e. PTR =
NULL.
21 / 38
Algorithm 2.5 : SEARCHULL (INFO, LOC, LINK, START, PTR, ITEM)
Let given linked list is sorted which stored in memory with
INFO and LINK. START is a starting pointer variable and PTR
is a local pointer variable which indicate the node which is
going to process. This algorithm finds the location LOC of the
node where ITEM first appear in the list or set LOC = NULL.
22 / 38
1. Sorted Searching Algorithm Complexity :-
a. The complexity of this algorithm is set as complete searching
algorithm for sorted linear array.
b. A sorted linear array we can apply binary search whose running
time is propositional to log2N.
- Let a linked list stored in memory using linear array INFO and pointer
array LINK.
- The linked representation of a linked list as follows,
STAR
15 50 10 20 35
START
INFO LINK
1 1 20 4
2 15
2 5
3 10
3 35 1
4
4 5 50 0
5 3
23 / 38
- From above representation, it is clear that one can’t insert any
element (node) in given linked list because there is no any memory
space present.
- Now suppose an element 20 is deleted from the list.
- We know in linked list deletion is take place only by changing.
- The link address of previous node.
- Here a previous node of node 20 is 10.
- Hence, in the LINK field of the node 1 10 will contain the address of
node 35, if one deleted node 20.
- Now though the element 20 is deleted still it is present physically in
the memory cell at location one.
- Hence, one can’t insert new element in this linked list.
- To overcome above problem, some mechanism is used which will
collects, memory space of deleted node are then available for future
use.
- A mechanism which performs such function is known as “Memory
Allocation”.
- In case of linked list, a memory allocation is perform by describing
another special list in the same arrays INFO and LINK.
- Here all the unused memory cells and the cells of deleted nodes will
be linked list by this special list.
- A variable AVAIL which is pointer of type of used to store beginning
location of the special linked list and invalid address NULL is used to
indicate the end of the special list.
- The special list is also known by the list of available space or free
storage list.
- A data structure with frequently will be denoted by writing : LIST
(INFO, LINK, START, AVAIL)
24 / 38
- Ex.: Consider a list of books is stored in linear array book & LINK then
available memory space in the linear array book may be linked as :
AVAIL
INFO LINK
1 1 DBMS 4
2
START
2 6
3 BASIC
3 5
4 HTML
4 5 C++ 0
6 5 1
6 0
- In above fig. AVAIL indicates first location i.e. 2 is the available space,
next 6 is available space.
2. Garbage Collection :
25 / 38
• Overflow and Underflow :
1. Overflow :
a. Overflow refers the situation one wants to insert the data into the
data structure, which is already filled.
b. This situation can be handle by printing message “OVERFLOW”.
c. In case of linked list overflow will occur when AVAIL = NULL.
2. Underflow :
a. Underflow refers a situation where one wants to delete data from a
empty data structure.
b. This situation can be handle by printing message “UNDERFLOW”.
26 / 38
Set NEW = AVAIL and AVAIL = LINK [AVAIL]
c. Place a new information ITEM into the new node as, Set
INFO[NEW] = ITEM.
ITEM
STAR
NEW
ITEM
27 / 38
Let LINK is a linked list with memory array INFO and LINK.
START and AVAIL is a link pointer variable which indicates
address of beginning with actual linked list. Here NEW is a
local pointer variable used to store address of node. This
algorithm perform insert an ITEM of information at the
beginning of the given linked list.
STAR
28 / 38
10 05 08 40
AVAIL
ITEM
Let linked list is stored in memory with INFO and LINK. START
and AVAIL is a LINK pointer variable which indicate the
address of beginning actual linked list. This algorithm perform
to insert ITEM after the given location LOC.
Otherwise is LOC = NULL insert at beginning.
29 / 38
Step 3.: [place ITEM into NEW node]
Set INFO[NEW] = ITEM
Step 4.: If LOC = NULL, then
[Insert as first node]
Set LINK[NEW] = START and
Set START = NEW
Else:
[Insert node after a given location]
Set LINK[NEW] = LINK[LOC] and
LINK[LOC] = NEW
[End of if structure]
Step 5.: [Finished] Exit.
- Suppose list is a sorted linked list stored in memory using linear array
INFO and LINK.
- When an ITEM is to be inserted into a sorted linked list, then it must
be satisfy the condition.
INFO[A] < ITEM < INFO[B]. Where A and B are two successive
nodes.
- To insert a NEW ITEM into a sorted linked list, one must know the
required location LOC of a node after which ITEM is to be inserted.
Procedure to find location LOC of a node is a sorted linked list, ITEM = 20.
LOC P PTR
STAR
9 10 15 25 30
- This procedure find location LOC of a node A. here value is < ITEM.
30 / 38
- The location is search by traversing a list using point variable PTR
and comparing ITEM with INFO[PTR].
- Here, while traversing the location of precidy node is save using a
pointer variable LOC P as shown in above figure.
- The pointer variable LOC and PTR is updated as,
Set LOCP = PTR and
PTR = LINK[PTR]
- Here, the traversing get stop as soon as ITEM < = INFO[PTR]
- If the above condition become true then this procedure set value of
LOC as,
LOC = LOCP and return.
- Also if given list is empty or ITEM < INFO[START] then it
Set LOC = NULL and return.
Procedure 2.2 : FINDLOCLL (INFO, LINK, START, ITEM, LOC)
31 / 38
[End of if structure]
Step 3.: [Initialize LOCP and PTR]
Set LOCP = START and
PTR = LINK[START]
Step 4.: [Make search]
32 / 38
Step 2.: [use algorithm 4.7 to insert ITEM after given
location]
Call INSERTFLOC(INFO, LINK, START, ITEM)
Step 3.: [Finished] Exit
STAR
Data List
Node A
Node B
10 05 50 20
AVAIL
33 / 38
- When deletion operation occurs, their must be to cases consider :
▪ If deleting the node X is the first node in the list then START
will point node B.
▪ If deleting node X is the last node in the list, then the LINK
pointer field of node A will contain a NULL pointer.
LOC
Node X
AVAIL
Let LIST be a linked list stored in memory with INFO and LINK.
LCO is a location of Node X which will delete and LOCP is a
location of the node precidy X. When X is the first node then
LOCP = NULL. This algorithm delete the node X with location
LOC.
34 / 38
Step 1.: If LOCP = NULL, then
Set START = LINK[START]
[Delete first node]
Else
Set LINK[LOCP] = LINK[LOC]
[Delete node X]
[End of if structure]
Step 2.: [Insert deleted node at the AVAIL list]
Set LINK[LOC] = AVAIL and
AVAIL = LOC
Step 3.: [Finished] Exit.
35 / 38
Procedure 4.2 : FINDLOCPLOC (INFO, LINK, START, PTR, LOCP, ITEM,
LOC)
This procedure find the location LOC of the first node X which
contains ITEM and location LCOP of the preceding node X. If
ITEM doesn’t appear in the LIST, then the procedure set LOC =
NULL and if ITEM appears in the first node then it sets LCOP =
NULL.
36 / 38
Set LOCP = SAVE and
LOC = PTR and Return
Else :
[update pointer SAVE and PTR]
Set SAVE = PTR and
PTR = LINK[PTR]
[End of if structure]
[End of step 4 loop]
Step 6. : [Set LOCP & LOC]
Write “Unsuccessful search”
Set LOCP = NULL and
LOC = NULL
Step 7.: Return. [Return to the point of call]
This algorithm deletion from linked list the first node X which
contain the ITEM of information.
37 / 38
Exit
Step 3.: [Is ITEM of node present at the beginning the list ?]
If LOCP = NULL, then
[Delete node]
Set START = LINK[START]
[End of if structure]
Step 4.: [Delete node from location LOC using preceding
node LOCP]
AVAIL LOC
Step 6.: [Finished] Exit.
38 / 38