Docsity

Sorting Algorithms & Data Structures: Bubble, Selection, Insertion, Quick, Merge Sort, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
i) BUBBLE SORT
Bubble sort method is the oldest and the simplest sorting method. It works by comparing each pair of
elements of the array and swapping (exchanging) them if they are not order. This process continues until
the entire array is sorted this sort method is given name "bubble sort" Because values float like a bubble
from one end of the array or list to another (just like a water bubble rises up to the water surface).
Suppose we are sorting an array of numbers in ascending order, higher values bubble out to the right
whereas lower values bubble out to the left side of array
Advantages of Bubble Sort
Bubble sort is generally considered to be the most inefficient sorting method because it is very slow for
sorting a large amount of data. However, it is simple and can easily be implemented. Usually, it is used
for sorting only a small amount of data.
ii)Selection Sort-
Selection sort is the simplest sorting method. It first finds the smallest value in the array and exchanges
this value with the value in the first position of the array, The it finds the second smallest value and
exchanges this value with the value in the position of the array and so on. This process continues until
the entire array is sorted.
Advantages of Selection Sort
Selection sort method is relatively easy to implement. It is also slightly faster than bubble sort because
selection sort makes only one exchange for every pass through the list of array. It is, however, inefficient
for larger lists
Disadvantages of Selection Sort
DATA STRUCTURE AND ALGORITHM
ASSIGNMENT # 01
Department:CS & IT
Course Instructor: PROFESSOR NADEEM CH Total Marks: 20
Name= MUHAMMAD ASIF
ROLLNO# : MCSE -F19-041
pf3
pf4
pf5

Partial preview of the text

Download Sorting Algorithms & Data Structures: Bubble, Selection, Insertion, Quick, Merge Sort and more Assignments Data Structures and Algorithms in PDF only on Docsity!

i) BUBBLE SORT Bubble sort method is the oldest and the simplest sorting method. It works by comparing each pair of elements of the array and swapping (exchanging) them if they are not order. This process continues until the entire array is sorted this sort method is given name "bubble sort" Because values float like a bubble from one end of the array or list to another (just like a water bubble rises up to the water surface). Suppose we are sorting an array of numbers in ascending order, higher values bubble out to the right whereas lower values bubble out to the left side of array Advantages of Bubble Sort Bubble sort is generally considered to be the most inefficient sorting method because it is very slow for sorting a large amount of data. However, it is simple and can easily be implemented. Usually, it is used for sorting only a small amount of data. ii)Selection Sort- Selection sort is the simplest sorting method. It first finds the smallest value in the array and exchanges this value with the value in the first position of the array, The it finds the second smallest value and exchanges this value with the value in the position of the array and so on. This process continues until the entire array is sorted. Advantages of Selection Sort Selection sort method is relatively easy to implement. It is also slightly faster than bubble sort because selection sort makes only one exchange for every pass through the list of array. It is, however, inefficient for larger lists Disadvantages of Selection Sort

DATA STRUCTURE AND ALGORITHM

ASSIGNMENT # 01

Department: CS & IT Course Instructor: PROFESSOR NADEEM CH Total Marks: 20 Name= MUHAMMAD ASIF ROLLNO# : MCSE -F19-

The primary disadvantage of the selection sort is its poor efficiency when dealing with a huge list of items. Similar to the bubble sort, the selection sort requires n-squared number of steps for sorting n elements. Additionally, its performance is easily influenced by the initial ordering of the items before the sorting process. Because of this, the selection sort is only suitable for a list of few elements that are in random order.

iii) Insertion Sort

The insertion sorts repeatedly scan the list of items, each time inserting the item in the unordered sequence into its correct position. Advantages of insertion Sort The main advantage of the insertion sort is its simplicity. It also exhibits a good performance when dealing with a small list. The insertion sort is an in-place sorting algorithm so the space requirement is minimal. Disadvantages of insertion Sort: The disadvantage of the insertion sort is that it does not perform as well as other, better sorting algorithms. With n-squared steps required for every n element to be sorted, the insertion sort does not deal well with a huge list. Therefore, the insertion sort is particularly useful only when sorting a list of few items. Quick Sort: The quick sort works on the divide-and-conquer principle. First, it partitions the list of items into two sub lists based on a pivot element. All elements in the first sub list are arranged to be smaller than the pivot, while all elements in the second sub list are arranged to be larger than the pivot. The same partitioning and arranging process is performed repeatedly on the resulting sub lists until the whole

list of items are sorted.

Advantages of Quick Sort: The quick sort is regarded as the best sorting algorithm. This is because of its significant advantage in terms of efficiency because it is able to deal well with a huge list of items. Because it sorts in place, no additional storage is required as well. Disadvantages of Quick Sort : The slight disadvantage of quick sort is that its worst-case performance is similar to average performances of the bubble, insertion or selections sorts. In general, the quick sort produces the most effective and widely used method of sorting a list of any item size.

Merge Sort:

Data Structures :

A particular way of storing and organizing data items in a computer's memory or any other

storage device so that it can be used efficiently is called data structure OR An arrangement of

data items in computer's memory is called data structure. The examples of common data

structures are arrays, linked lists, queues, stack, binary tree, and hash tables.

Types of Data Structures

Following are major types of data structures

i) Linear Data Structure

ii) Non-Linear Data Structure

Linear Data Structure

In linear data structure, data items are arranged in a sequence or order (or liner fashion Linear

data structure can be constructed as a continuous arrangement of data items or elements in

the memory, Arrays, linked lists, stacks and queues are examples of linear data structures in

which data items are stored in a sequence and can be accessed sequentially.

Non-Linear Data Structure

Non-linear data structure is opposite to linear data structure. Is this type of data structure, data

items are not in sequence or order .Non linear data structure can be constructed as a collection

of randomly distributed set of data items or elements.

1.1 Arrays Arrays are a homogeneous and contiguous collection of same data types. They have a static memory allocation technique, which means, if memory space is allocated for once, it cannot be changed during runtime. The arrays are used to implement vectors, matrices and also other data structures. If we do not know the memory to be allocated in advance then array can lead to wastage of memory. Also, insertions and deletions are complex in arrays since elements are stored in consecutive memory allocations.

2.1 Lists The lists support dynamic memory allocation. The memory space allocated can be changed at run time also. The lists are of two types: a) Linear Lists The linear lists are those which have the elements stored in a sequential order. The insertions and deletions are easier in the lists. They are divided into two types:  Stacks: The stack follows a “LIFO” technique for storing and retrieving elements. The element which is stored at the end will be the first one to be retrieved from the stack. The stack has the following primary functions:  Push(): To insert an element in the stack.  Pop(): To remove an element from the stack.  Queues: The queues follow “FIFO” mechanism for storing and retrieving elements. The elements which are stored first into the queue will only be the first elements to be removed out from the queue. The “ENQUEUE” operation is used to insert an element into the queue whereas the “DEQUEUE” operation is used to remove an element from the queue. b) Non Linear Lists The non linear lists do not have elements stored in a certain manner. These are:  Graphs: The Graph data structure is used to represent a network. It comprises of vertices and edges (to connect the vertices). The graphs are very useful when it comes to study a network. Trees: Tree data structure comprises of nodes connected in a particular arrangement and they (particularly binary trees) make search operations on the data items easy. The tree data structures consist of a root node which is further divided into various child nodes and so on. The number of levels of the tree is also called height of the tree.