0% found this document useful (0 votes)
11 views11 pages

Day 3

Uploaded by

wolfmusashix09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

Day 3

Uploaded by

wolfmusashix09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Data Structure

Trainer : Nisha Dingare


Email : nisha.dingare@sunbeaminfo.com

Sunbeam Infotech www.sunbeaminfo.com


Merge Sort :

• This algorithm follows divide-and-conquer approach.


• In this algorithm, big size array is divided logically into smallest size (i.e. having size 1) subarrays, after
dividing array into smallest size subarray’s, subarrays gets merged into one array step by step in a
sorted manner and finally all array elements gets arranged in a sorted manner. –
• This algorithm works fine for even as well odd input size array. -This algorithm takes extra space to
sort array elements, and hence its space complexity is more.

• Time Complexity :
• Best Case : Ω(n log n)
• Worst Case : O(n log n)
• Average Case : θ(n log n)

Sunbeam Infotech www.sunbeaminfo.com


Merge Sort :

Sunbeam Infotech www.sunbeaminfo.com


Quick Sort :

• This algorithm follows divide-and-conquer approach.


• In this algorithm the basic logic is a partitioning.
• Partitioning: in partitioning, pivot element gets selected first (it may be either leftmost or rightmost or middle
most element in an array).
• After selection of pivot element all the elements which are smaller than pivot gets arranged towards the left
and elements which are greater than pivot gets arranged to the right.
• Next, The big size array is divided into two subarray’s, so after first pass pivot element gets settled at its
appropriate position. elements which are at left of pivot is referred as left partition and elements which are at
its right referred as a right partition.
• Quick sort algorithm is an efficient sorting algorithm for larger input size array.

• Time Complexity :
• Best Case : Ω(n log n)
• Worst Case : O(n2) – worst case rarely occurs
• Average Case : θ(n log n)

Sunbeam Infotech www.sunbeaminfo.com


Linked List - Introduction :

• Like Arrays , Linked List is a LINEAR DATA STRUCTURE.


• But unlike arrays, Linked Lists are NOT stored in contiguous memory locations.
• The items in the linked lists are connected to one another using pointers.
• These items are called as NODES.
• Each node contains 2 parts :
• Data part : it contains actual data (primitive or non-primitive data).
• Next part : it is a pointer which contains the address of the next node.
• Address of the first node is stored in the Head pointer.

*Head Data *Next Data *Next Data *Next Data *Next


100 11 200 22 300 33 400 44 NULL

100 200 300 400

Sunbeam Infotech www.sunbeaminfo.com


Linked Lists :

• Why Linked List ?


• Linked Lists are dynamically allocated as and when required and released after it serves its purpose. so
unlike arrays they can grow or shrink in size at runtime.
• Adding or deleting an item in a linked list is much efficient and easier compared to arrays.

• Types of Linked Lists :


1. Singly Linear Linked List
2. Singly Circular Linked List
3. Doubly Linear Linked List
4. Doubly Circular Linked List

Sunbeam Infotech www.sunbeaminfo.com


Singly Linear Linked List :
• This linked list is also called as simple linked list.
• It is a type of linked list in which the list can be traversed only in one direction.
• In this, the pointer of each node points to other nodes whereas the pointer of last node contains NULL.
• Address of first node is stored in the Head pointer.
*Head Data *Next Data *Next Data *Next Data *Next
100 11 200 22 300 33 400 44 NULL

100 200 300 400


• Operations performed on linked list :
• Add node at first position
• Add node at last position
• Add node at specific position
• Delete node at first position
• Delete node at last position
• Delete node at specific position
• Traverse the list

Sunbeam Infotech www.sunbeaminfo.com


Singly Linear Linked List - Add at Last position :

• If Head is NULL, Add the new node to the Head.


Head* Data Next*
500 55 NULL
NULL
500

• Else, Traverse the list till the last node and add the address of new node to the next part of the last
node.

Head* Data Next* Data Next* Data Next* Data Next*


100 500
11 200 22 300 33 400 44 NULL
100 200 300 400 Data Next*

Trav* 55 NULL

400 500

Sunbeam Infotech www.sunbeaminfo.com


Singly Linear Linked List – Add at First position :

• If Head is NULL, Add the new node to the Head.


*Head Data *Next
500 55 NULL
NULL
500

• To add a new node at the first position always create a new connection first between the new node
and the already existing first node. Then change the address inside head pointer to point to the new
node. So the Rule is MAKE BEFORE BREAK.

*Head Data *Next Data *Next Data *Next Data *Next


500
100 11 200 22 300 33 400 44 NULL

100 200 300 400


Data *Next
55 NULL
100
500
Sunbeam Infotech www.sunbeaminfo.com
Singly Linear Linked List – Add at Specific position

*Head Data *Next Data *Next Data *Next Data *Next


100 11 200 22 500 33 400 44 NULL

100 200 300 400


Data *Next
If pos = 3 55 NULL
300
Start the traversal from 1st node till pos-1
500
Next,
Store address of current pos node into next part of newnode
newnode->next=trav->next
Store an address of newly created node into next part of (pos-1)th node
Trav->next = newnode;

Sunbeam Infotech www.sunbeaminfo.com


Thank You !!

Sunbeam Infotech www.sunbeaminfo.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy