0% found this document useful (0 votes)
4 views

Data Structures - Algorithms

An algorithm is a step-by-step procedure for solving a problem, consisting of input, processing, and output. Key characteristics include clarity, well-defined inputs and outputs, finiteness, feasibility, and language independence. The document also discusses various types of algorithms, how to write and express them, and provides examples of sorting algorithms like bubble sort, insertion sort, and quick sort.

Uploaded by

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

Data Structures - Algorithms

An algorithm is a step-by-step procedure for solving a problem, consisting of input, processing, and output. Key characteristics include clarity, well-defined inputs and outputs, finiteness, feasibility, and language independence. The document also discusses various types of algorithms, how to write and express them, and provides examples of sorting algorithms like bubble sort, insertion sort, and quick sort.

Uploaded by

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

Algorithms

An algorithm is a step-by-step procedure for solving a problem or accomplishing a task. They


are a set of well-defined instructions for performing a specific computational task.

Algorithms have three components: input, process and output.


 Input: The algorithm receives input data.
 Processing: The algorithm performs a series of operations on the input data.
 Output: The algorithm produces the desired output.
Characteristics of an algorithm:
 Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps
should be clear in all aspects and must lead to only one meaning.
 Well-defined Inputs: If an algorithm says to take inputs, it should be well-defined
inputs. It may or may not take input.
 Well-defined Outputs: The algorithm must clearly define what output will be yielded
and it should be well-defined as well. It should produce at least 1 output.
 Finiteness: The algorithm must be finite, i.e. it should terminate after a finite time.
 Feasible: The algorithm must be simple, generic, and practical, such that it can be
executed using reasonable constraints and resources.
 Language Independent: Algorithm must be language-independent, i.e. it must be just
plain instructions that can be implemented in any language, and yet the output will
be the same, as expected.

How to Write an Algorithm?


To write an algorithm, follow these steps:
 Define the problem: Clearly state the problem to be solved.
 Design the algorithm: Choose an appropriate algorithm design and develop a step-by-
step procedure.
 Implement the algorithm: Translate the algorithm into a programming language.
 Test and debug: Execute the algorithm with various inputs to ensure its correctness
and efficiency.
 Analyse the algorithm: Determine its time and space complexity and compare it to
alternative algorithms.
Types of Algorithms:
1. Brute Force Algorithm :
 It is the simplest approach to a problem. A brute force algorithm is the first
approach that comes to finding when we see a problem
2. Recursive Algorithm:
A recursive algorithm is based on recursion. A problem is broken into several sub-parts and
called the same function again and again.
3. Backtracking Algorithm:
The backtracking algorithm builds the solution by searching among all possible solutions.
Whenever a solution fails we trace back to the failure point build on the next solution and
continue this process till we find the solution or all possible solutions are looked after.

4. Searching Algorithm:
Searching algorithms are the ones that are used for searching elements or groups of elements
from a particular data structure.

5. Sorting Algorithm:
Sorting is arranging a group of data in a particular manner according to the requirement.
Sorting algorithms are used to sort groups of data in an increasing or decreasing manner.

6. Hashing Algorithm:
Hashing algorithms work similarly to the searching algorithm. But they contain an index with
a key ID. A key is assigned to specific data.

7. Divide and Conquer Algorithm:


This algorithm breaks a problem into sub-problems, solves a single sub-problem, and merges
the solutions to get the final solution. It consists of the following three steps:
 Divide
 Solve
 Combine
8. Greedy Algorithm:
The solution is built part by part. The solution for the next part is built based on the
immediate benefit of the next part. The one solution that gives the most benefit will be chosen
as the solution for the next part.
9. Dynamic Programming Algorithm:
Uses the concept of using the already found solution to avoid repetitive calculation of the
same part of the problem. It divides the problem into smaller overlapping subproblems and
solves them.

How to express an Algorithm?


1. Natural Language:- Here we express the Algorithm in the natural English language. It
is too hard to understand the algorithm from it.
2. Flowchart:- Here we express the Algorithm by making a graphical/pictorial
representation of it. It is easier to understand than Natural Language.
3. Pseudo Code:- Here we express the Algorithm in the form of annotations and
informative text written in plain English which is very much similar to the real code
but as it has no syntax like any of the programming languages, it can’t be compiled or
interpreted by the computer. It is the best way to express an algorithm because it can
be understood by even a layman with some school-level knowledge.

Sorting
Sorting refers to rearrangement of a given array or list of elements in ascending or
descending order. Sorting reduces the complexity of a problem. The importance of sorting
lies in the fact that data searching can be optimized to a very high level. Sorting is also used
to represent data in more readable formats. We are going to look at bubble sort, insertion sort
and quick sort.

1. Bubble sort
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based
algorithm in which each pair of adjacent elements is compared and the elements are swapped
if they are not in order. This algorithm is not suitable for large data sets.

Bubble Sort Algorithm


Bubble Sort is an basic sorting algorithm, which works by repeatedly exchanging adjacent
elements, if necessary. When no exchanges are required, the file is sorted.
We assume list is an array of n elements. We further assume that swap function swaps the
values of the given array elements.
Step 1 − Check if the first element in the input array is greater than the next element in the
array.
Step 2 − If it is greater, swap the two elements; otherwise move the pointer forward in the
array.
Step 3 − Repeat Step 2 until we reach the end of the array.
Step 4 − Check if the elements are sorted; if not, repeat the same process (Step 1 to Step 3)
from the last element of the array to the first.
Step 5 − The final output achieved is the sorted array.

Example
We take an unsorted array for our example.

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.
Next we compare 33 and 35. We find that both are in already sorted positions.

Then we move to the next two values, 35 and 10.

We know then that 10 is smaller 35. Hence they are not sorted. We swap these values. We
find that we have reached the end of the array. After one iteration, the array should look like
this −

To be precise, we are now showing how an array should look like after each iteration. After
the second iteration, it should look like this −
And when there's no swap required, bubble sort learns that an array is completely sorted.

2. Insertion Sort Algorithm


The insertion sort algorithm works by building up a sorted sublist in the first part of the
original list, while the remaining part of the list remains unsorted. At the start of the
algorithm, the sorted sublist contains just a single item (the first item in the list). All of the
other items belong to the unsorted sublist. The algorithm goes through the unsorted sublist,
item by item. As each item is examined, it is moved into the correct position (in ascending or
descending order) in the sorted sublist. This progresses until the final item is correctly
inserted and the list is sorted.
Each time the algorithm goes through the sorted sublist and places an unsorted item into the
correct position it is called a pass
Each time the algorithm compares an item from the unsorted sublist with an item from the
sorted sublist to check if a position for the item to insert has been found it is called
a comparison

Example
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 the correct position. It swaps 33 with 27. It also checks with all the
elements of sorted sub-list. Here we see that the sorted sub-list has only one element 14, and
27 is greater than 14. Hence, the sorted sub-list remains sorted after swapping.

By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10. These values
are not in a sorted order.
So they are swapped.

However, swapping makes 27 and 10 unsorted.

Hence, we swap them too.

Again we find 14 and 10 in an unsorted order.

We swap them again.

By the end of third iteration, we have a sorted sub-list of 4 items.

This process goes on until all the unsorted values are covered in a sorted sub-list. Now we
shall see some programming aspects of insertion sort.
Quick sort
Quick sort uses the technique of divide-and-conquer. Quick sort is a highly efficient sorting
algorithm and is based on partitioning of array of data into smaller arrays. A large array is
partitioned into two arrays one of which holds values smaller than the specified value, say
pivot, based on which the partition is made and another array holds values greater than the
pivot value. Quicksort partitions an array and then calls itself recursively twice to sort the two
resulting subarrays. This algorithm is quite efficient for large-sized data sets.
We proceed as follows:
1. Pick an arbitrary element of the array (the pivot).
2. Divide the array into two segments, those that are smaller and those that are greater, with
the pivot in between (the partition phase).
3. Recursively sort the segments to the left and right of the pivot.

Say we have the array


3, 1, 4, 4, 7, 2, 8

and we pick 3 as our pivot. Then we have to compare each element of this (unsorted!) array
to the pivot to obtain a partition where 2, 1 are to the left and 4, 7, 8, 4 are to the right of the
pivot. Then the two segments containing (2, 1) and (4, 7, 8, 4) are further divided using an
arbitrary figure as a pivot until all the segments are sorted.

You might also like