0% found this document useful (0 votes)
3 views31 pages

data structure 1

The document outlines various algorithms for insertion, deletion, searching, and sorting in arrays, detailing their best, worst, and average time complexities. It includes algorithms for insertion and deletion at a specified position, finding maximum/minimum values, linear and binary search, and several sorting techniques such as bubble sort, selection sort, insertion sort, and merge sort. Each algorithm is accompanied by a step-by-step explanation and pseudocode for implementation.

Uploaded by

Mohit Sidana
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)
3 views31 pages

data structure 1

The document outlines various algorithms for insertion, deletion, searching, and sorting in arrays, detailing their best, worst, and average time complexities. It includes algorithms for insertion and deletion at a specified position, finding maximum/minimum values, linear and binary search, and several sorting techniques such as bubble sort, selection sort, insertion sort, and merge sort. Each algorithm is accompanied by a step-by-step explanation and pseudocode for implementation.

Uploaded by

Mohit Sidana
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/ 31

ALL MAJOR QUESTIONS FROM ALGO & PROGRAMMING

Insertion in an array (at Kth position)


Best Case:
• O(1): If insertion is at the end of the array (i.e., k = N), we don't need to shift
any elements, and insertion takes constant time.
Worst Case:
• O(N): If insertion is at the beginning of the array (i.e., k = 1), we need to
shift all elements one position to the right, making the time complexity
linear with respect to the number of elements in the array.
Average Case:
• O(N): On average, insertion would occur somewhere in the middle, and
about half of the array elements need to be shifted. The complexity remains
linear, i.e., O(N).
Algorithm:
INSERTION(A,N,K,ITEM): This algorithm is used to insert an element in linear array
A of size N. ITEM is the element which is needed to be inserted at Kth Position of
the array. Here I is acting as a temporary variable.
Step 1: START
Step 2: FOR I=N down to K
A(I)=A(I-1)
END FOR
Step 3: A(K)=ITEM
Step 4: N=N+1
Step 5: FOR I=1 to N
PRINT "A(I)"
Step 6: END
Program:

-------------------------------------------------------------------------------------------------------------
Deletion in an array (at Kth Position)
Best Case:
• O(1): If deletion is at the end of the array (i.e., k = N), we don't need to shift
any elements, and deletion takes constant time.
Worst Case:
• O(N): If deletion is at the beginning of the array (i.e., k = 1), all elements
must be shifted one position to the left, resulting in a linear time
complexity.
Average Case:
• O(N): On average, deletion occurs somewhere in the middle, so about half
the array elements need to be shifted. The time complexity remains linear,
i.e., O(N).
Algorithm:
DELETION(A,N,K): This algorithm is used to delete an element in linear array A of
size N. Kth is the position where element is deleted. Here I is acting as a
temporary variable.
Step 1: START
Step 2: FOR I=K to N-1
A(I)=A(I+1)
END FOR
Step 3: N=N-1
Step 4: FOR I=1 to N
PRINT "A(I)"
Step 5: END
Program:
-------------------------------------------------------------------------------------------------------------
MAX/MIN from array
Best Case: O(N)
• Even in the best case, where the minimum or maximum is the first element
in the array, you still need to traverse the entire array to confirm that there
isn't a smaller or larger element.
• Therefore, the algorithm still checks every element in the array and has a
time complexity of O(N).
Worst Case: O(N)
• In the worst case, the minimum (or maximum) element is the last element
in the array. You would still need to check every element to find this out,
leading to N - 1 comparisons.
• So, the time complexity remains O(N).
Average Case: O(N)
• On average, the minimum or maximum element could be located anywhere
in the array (first, middle, last, etc.).
• However, regardless of the position, the algorithm will still need to check
every element to ensure it finds the minimum or maximum.
• Hence, the average case also has a time complexity of O(N).
Algorithm:
MAX(A, N): This algorithm will find maximum element from array A of N elements.
Here I and MAX are temporary variables.
Start 1: START
Start 2: Set MAX to the first element of the array (MAX=A[1])
Step 3: FOR I=2 to N DO:
IF A[I]>MAX then:
Set MAX=A[I]
Step 4: PRINT MAX
Step 5: END
Program:
-------------------------------------------------------------------------------------------------------------
Linear Search
Linear Search, also known as sequential search, is one of the simplest and most fundamental searching
algorithms. It is designed to find the position of a specific target element (often referred to as "key" or
"item") within a list or array. The algorithm works by systematically scanning through each element of
the data structure from the beginning to the end, comparing each element with the target value. If a
match is found, the search terminates successfully, and the index of the found element is returned. If no
match is found after examining all elements, the algorithm concludes that the target does not exist
within the array.

One of the key advantages of linear search is that it can be applied to both sorted and unsorted arrays.
This makes it a versatile option when dealing with unsorted data, where more advanced algorithms like
binary search cannot be used.

Time Complexity
1. Best Case (O(1)):

• Explanation: The best-case scenario occurs when the target element is found at the very first
position of the array.
2. Worst Case (O(n)):

• Explanation: The worst-case scenario occurs when the target element is either not present in
the array or is the last element in the array.

3. Average Case (O(n)):

• Explanation: On average, the target element might be located around the middle of the array, so
the algorithm would have to check about half the elements, but the complexity remains O(n)
because we drop constants.

Algorithm:
LINEAR_SEARCH (A,N,ITEM): Here A is a linear array of N elements. Here ITEM represents the value to be
searched. Here we use I as temporary variable.

Step 1: Start

Step 2: FOR I=1 to N do

IF A[I]=ITEM then

PRINT “Item found”

Go to step 3

END IF

END FOR

Step 3: IF I=N then

PRINT “item is not in the array”

END IF

Step 4: END

Program:
------------------------------------------------------------------------------------------------------------------------------------------

Binary Search
Binary Search is an efficient searching algorithm used to find the position of a specific target value within
a sorted array or list. Unlike linear search, which checks every element sequentially, binary search
drastically reduces the number of comparisons needed by taking advantage of the sorted order of
elements. It works by repeatedly dividing the search interval in half, making it highly efficient for large
datasets.

Binary search operates under the principle of divide and conquer. It compares the target element with
the middle element of the array:

• If the target is equal to the middle element, the search is complete, and the index is returned.
• If the target is smaller than the middle element, the algorithm narrows the search to the lower
half of the array.

• If the target is larger than the middle element, the search continues in the upper half. This
process is repeated until the target is found or the search interval becomes empty, indicating
that the target is not present in the array.

Time Complexity:
Best Case: O(1) — The target is found at the middle on the first comparison.

Worst Case: O(log N) — The target is found after repeatedly halving the array

Average Case: O(log N) — The target is equally likely to be anywhere in the array.

Algorithm:

BINARY_SEARCH (A,N,ITEM): This algorithm is used to search ITEM value from a sorted Linear
Array A of N elements. Here we use BEG, END, MID denotes beginning, ending and middle
positions and are Temporary Variables.

Step 1: START

Step 2: Set BEG=1, END=N

Step 3: Repeat WHILE BEG < = END

Calculate MID = (BEG + END)/2;

IF ITEM=A(MID) then

Go to step 5
ELSE IF ITEM < A(MID) then

Set END=MID-1

ELSE

Set BEG=MID+1

END IF

END WHILE

Step 4: IF BEG > END then

PRINT “item not found”

Step 5: END

Program:
-------------------------------------------------------------------------------------------------------------------------------

Bubble Sort

Bubble Sort is one of the simplest sorting algorithms used to arrange elements in ascending or
descending order. The algorithm repeatedly compares adjacent elements in the array and
swaps them if they are in the wrong order. This process continues until the entire array is
sorted. The name "Bubble Sort" comes from the way larger elements "bubble up" to the end of
the list, while smaller elements "sink" to the beginning with each pass through the array.

How Bubble Sort Works:

1. Repeated Comparison and Swapping: The algorithm starts at the beginning of the array
and compares each pair of adjacent elements. If the first element is greater than the
second, they are swapped. This comparison and swapping continue through the entire
array.
2. After each pass through the array, the largest unsorted element moves to its correct
position (at the end of the list).
3. The process is repeated for all elements, but after each pass, the number of
comparisons decreases, as the largest elements have already been moved to their
correct positions.
4. Early Exit Condition: If during any pass, no elements are swapped, it means the array is
already sorted, and the algorithm can terminate early.

Time Complexity:

1) Best Case: O(N) — The array is already sorted.


2) Average Case: O(N²) — Random order of elements. Therefore multiple comparisons and
are quadratic
3) Worst Case: O(N²) — The array is sorted in reverse order.
Algorithm:

BUBBLE_SORT(A,N): This algorithm sorts a linear array A of N elements using bubble sort
technique. Here we use I,J,K as temporary variables.

Step 1: START

Step 2: FOR I=1 to N-1 DO

FOR J=1 to N-I DO

IF A(J) > A(J+1) then

Set K=A(J)

Set A(J)=A(J+1)

Set A(J+1)=K

END IF

END FOR (inner)

END FOR (outer)

Step 3: END

Program:
------------------------------------------------------------------------------------------------------------------------------

Selection Sort
Selection Sort is one of the simplest sorting algorithms that works by repeatedly
finding the minimum (or maximum) element from the unsorted part of the array
and placing it at the beginning (or end) of the array. The algorithm maintains two
subarrays within the array:
1. Sorted subarray – which is built up from left to right, element by element.
2. Unsorted subarray – which contains the remaining elements that need to
be sorted.
How Selection Sort Works:
1. Step 1 (Initialization): The algorithm starts by considering the entire array
as unsorted and sets the first element of the array as the "current position."
2. Step 2 (Find Minimum Element): It then searches through the unsorted
subarray to find the smallest element.
3. Step 3 (Swap): Once the smallest element is found, it swaps this element
with the first element of the unsorted subarray (i.e., the current position).
This moves the smallest element to its correct position in the sorted
subarray.
4. Step 4 (Repeat): The algorithm repeats the process for the rest of the
unsorted subarray by moving the current position to the next element,
continuing until the array is completely sorted.
Time Complexity:
• Best Case: O(N²)
• Worst Case: O(N²)
• Average Case: O(N²)
Explanation:
Selection sort always requires O(N²) comparisons regardless of the initial order of
elements in the array.
Algorithm:
SELECTION_SORT(A,N): This algorithm is used to do sorting on array A of N
elements. I,P,K,TEMP are used as a temporary variable.
Step 1: START
Step 2: FOR P=1 to N-1 DO
K=P
FOR I=P+1 to N DO
IF A(I)<A(K) then
K=I
END IF
END FOR
TEMP=A(P)
A(P)=A(K)
A(K)=TEMP
END FOR
Step 3: END
Program:
-------------------------------------------------------------------------------------------------------------------------------

Insertion Sort
Insertion Sort is a simple and intuitive sorting algorithm that builds a sorted array
(or list) one element at a time. It is much less efficient on large lists than more
advanced algorithms such as quicksort, heapsort, or merge sort, but it has several
advantages, including simplicity and efficiency for small datasets.
How Insertion Sort Works
The algorithm works similarly to the way you might sort playing cards in your
hands. Here’s a step-by-step explanation of its working:
1. Initial Setup:
o Start with the second element of the array (the first element is
assumed to be sorted).
o Assume that the left part of the array (from the first element to the
current element) is already sorted.
2. Key Steps:
o Select an Element: For each element, referred to as the "key," start
from the second element to the last element of the array.
o Comparison: Compare the key with the elements in the sorted
section (to its left). If the key is smaller than the compared element,
shift the compared element one position to the right.
o Insertion: Once the correct position for the key is found (when you
find an element that is smaller than or equal to the key), insert the
key at that position.
3. Repeat: Repeat this process for all elements in the array until the entire
array is sorted.
Time Complexity:
• Best Case: O(N) — Occurs when the array is already sorted.
• Worst Case: O(N²) — Occurs when the array is sorted in reverse order.
• Average Case: O(N²) — Occurs for a randomly ordered array.

Algorithm:
INSERTION_SORT(A,N): This algorithm is used to sort elements of an array A
having N elements. Here I,J,TEMP act as a temporary variables.
Step 1: START
Step 2: FOR I=2 to N DO:
TEMP=A(I)
J=I-1
REPEAT WHILE (TEMP<A(J) AND J>=1) DO
A(J+1) = A(J)
J=J-1
END WHILE
A(J+1) = TEMP
END FOR
Step 3: END
Program:
-------------------------------------------------------------------------------------------------------------
Merge Sort
Merge Sort is a classic sorting algorithm that follows the divide and conquer
paradigm. It is particularly efficient for large datasets and is based on the idea of
dividing the input array into two halves, sorting each half, and then merging the
sorted halves back together. Here’s a detailed breakdown of Merge Sort, its
working, and its advantages.
How Merge Sort Works
1. Divide:
o If the array has one or no elements, it is already sorted. If it has more
than one element, the array is split into two halves. This division
continues recursively until we reach arrays of size one.
2. Conquer:
o Each half is sorted recursively using the same merge sort algorithm.
3. Combine (Merge):
o The two sorted halves are then merged back together into a single
sorted array. During the merge process, we compare the elements of
the two halves and arrange them in order.
Time Complexity:
• Best Case: O(N log N) — This occurs regardless of the input arrangement.
• Worst Case: O(N log N) — The worst-case scenario also requires the same
number of comparisons and merges.
• Average Case: O(N log N) — The average case behaves similarly to the worst
case due to the consistent division and merging process.
Algorithm:
MERGESORT(A,LOW,HIGH): This algorithm will sort an array of A of N elements. It
will sort this using merge sort technique. Here LOW and HIGH mark starting and
ending position of array A. Here MID is used as temporary variable.
Step 1: START
Step 2: IF LOW<HIGH then
Set MID = (LOW+HIGH)/2
Call MERGESORT (A,LOW,MID)
Call MERGESORT (A, MID+1, HIGH)
Call MERGE (A, LOW, MID, HIGH)
END IF
Step 3: END
MERGE(A,LOW,MID,HIGH): This algorithm will merge two sorted sub lists into a
single sorted list. Here A is a linear array. LOW, MID & HIGH represents starting,
mid & ending positions respectively. Here we use I, J, K as temporary variables & B
is used as temporary array.
Step 1: START
Step 2: Set I=LOW, J=MID +1, K=LOW
Step 3: Repeat while (I<=MID AND J<=HIGH) DO
IF( A(I)<A(J) ) then
Set B(K)=A(I)
Set k=k+1
Set I=I+1
ELSE
Set B(K)=A(J)
Set K=K+1
Set J=J+1
END IF
END WHILE
Step 4: Repeat WHILE (I<=MID) DO
Set B(K)=A(I)
Set K=K+1
Set I=I+1
END WHILE
Step 5: Repeat WHILE(J<=HIGH) DO
Set B(K)=A(J)
Set K=K+1
Set J=J+1
END WHILE
Step 6: FOR I=LOW TO HIGH DO
Set A(I)=B(I)
END FOR
Step 7: END
Program:
-------------------------------------------------------------------------------------------------------------------------------

Quick Sort
Quick Sort is a highly efficient sorting algorithm that uses the divide-and-conquer
approach to sort an array or a list. It is known for its average-case time complexity
of O(N log N), making it one of the fastest sorting algorithms for large datasets.
Key Concepts
1. Divide and Conquer: The algorithm divides the array into smaller sub-
arrays, sorts them independently, and then combines the results.
2. Pivot Element: A pivot element is chosen from the array. The choice of
pivot can affect the performance of the algorithm.
3. Partitioning: The array is rearranged so that elements less than the pivot
are on its left, and elements greater than the pivot are on its right.
4. Recursive Sorting: The process is repeated for the sub-arrays until they are
sorted.
Time Complexity:
• Best Case: O(N log N) - When the pivot divides the array into two equal
halves.
• Average Case: O(N log N) - Typically observed with random data.
• Worst Case: O(N^2) - When the smallest or largest element is consistently
chosen as the pivot, resulting in unbalanced partitions (e.g., sorted or
reverse-sorted arrays).

Algorithm:
QUICKSORT(A,LOW,HIGH): This algorithm will sort an Array A of N elements. It will
sort using quicksort technique. Here LOW and HIGH are starting and ending
position of Array A. Here J is a temporary variable.
Step 1: START
Step 2: IF LOW<HIGH then
Set J=PARTITION(A, LOW, HIGH)
Call QUICKSORT(A, LOW, J-1)
Call QUICKSORT(A, J+1, HIGH)
END IF
Step 3: STOP
PARTITION(A, LOW, HIGH): This algorithm will do partitioning and it will be done
by J temporary variable. Left of J will have elements less than J and right of J will
have elements greater than J. A is a linear Array A. I, J PIVOT & K are temporary
variables.
Step 1: START
Step 2: PIVOT = A(LOW), I =LOW, J = HIGH + 1
Step 3: Repeat DO WHILE (I<=J) DO
Repeat DO WHILE ( A(I)<PIVOT ) DO
I=I+1
END DO WHILE
Repeat DO WHILE ( A(J)>PIVOT ) DO
J=J-1
END DO WHILE
IF(I<J) DO
K=A(J)
A(I)=A(J)
A(J)=K
END IF
END DO WHILE
Step 4: A(LOW)=A(J)
A(J)=PIVOT
return J
Step 5: END
Program:

You might also like