You are on page 1of 15

SDU

Search and sorting algorithms

Karimov Adilbek
ICT
Aidana Batyrkhanova
18.10.2020
Hello! Today we are going to talk about search
and sorting algorithms!
What is sorting algorithms?
A sorting algorithm is an algorithm for ordering
items in a list. In the case when a list item has
several fields, the field serving as the order
criterion is called the sort key.
What is searching algorithms?
In computer science, a search algorithm is any
algorithm that solves a search problem, namely,
to retrieve information stored in some data
structure or computed in the problem domain
search space, either with discrete or continuous
values.
Our task is to tell about types of searching and
sorting algorithms! So, let's begin!
1. Linear/Binary search
Linear search - finding the required element in a certain list.
Imagine this task - you need to translate the word "computer" into
Russian, and the only tool you have is a bad English-Russian dictionary.
The bad thing is that the words inside the dictionary are not sorted
according to the alphabet, they are scattered chaotically. Now think
about what you need to do to find the word "computer" in this
dictionary?
The correct answer is to look at all the words in the dictionary one by
one and hope that the word will be at the beginning of the dictionary,
and not at the end.
This is what linear search is, you have a set of elements where you need
to find a specific element. Try to write your own linear search function
where you are given a list of integers, and you need to tell if this word is
in the list or not.
Binary search
Let's make a small change to the last, task, you also need to find number
in the list, but this time the numbers in the list are sorted in non-
decreasing order, those. Each number is not less than the previous
number in the list - 0, 0, 1, 2, 2, 3, 10, 11. Agree that looking for a
number by linear search is not the smartest decision at the moment. If
you think a little, you can come up with a new way of searching - binary
search. A prerequisite for searching is data sorted.
Example: List - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], desired number - 3. Binary
search algorithm: We divide the list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] into two
parts - [0, 1, 2, 3, 4] and [5, 6, 7, 8, 9] .The middle element is 4, it is
more than the desired number, you can guaranteed discard the right
side .We divide the list [0, 1, 2, 3, 4] into two parts - [0, 1, 2] and [3,
4] .The middle element is 2, it is less than the required number, you can
reliably discard the left side. We divide the list [3, 4] into two parts - [3]
and [4].
The middle element is 3, it is equal to the desired element, our work is
finished.

2.Bubble sort
This sorting method is also quite simple to understand and write code.
Unlike selection sort, bubble sort rearranges the maximum elements of
the list to their places, and simultaneously changes in places the
elements that are wrong. The whole algorithm consists in the fact that
we go through the list several times, and swap adjacent elements if they
are wrong.
Example:
(5 1 4 2 8) → (1 5 4 2 8) Compare the first two elements and swap them,
since 5> 1
(1 5 4 2 8) → (1 4 5 2 8) Change places, since 5> 4
(1 4 5 2 8) → (1 4 2 5 8) Change places, since 5> 2
(1 4 2 5 8) → (1 4 2 5 8) Do not swap, since 5 <8, this is how it should
be
(1 4 2 5 8) → (1 4 2 5 8)
(1 4 2 5 8) → (1 2 4 5 8) Change places, since 4> 2
(1 2 4 5 8) → (1 2 4 5 8)
(1 2 4 5 8) → (1 2 4 5 8) The list is sorted.
In this case, two iterations (running through the list) were enough to sort
it, but this is not a rule, since the number of iterations depends on how
much the list is unsorted. In general, if the list contains n elements, n - 1
iterations are enough, you can clearly see this by sorting the list (5, 4, 3,
2, 1).
3. Insertion sort
Insert Sort is a simple sorting algorithm that works in a similar way to
how you sort playing cards in your hand. The array is virtually split into
sorted and unsorted parts. The values from the unsorted part are fetched
and placed in the correct position in the sorted part.
Algorithm:
To sort an array of size n in ascending order:
1. Jump from arr [1] to arr [n] by array.
2. Compare the current item (key) with its predecessor.
3. If the key element is smaller than its predecessor, compare it with the
previous elements. Move large items up one position to make room for
the replaced item.
4. Quick sort
Like Insertion Sort, quick sort is a divide and Conquer algorithm. It
picks an element as pivot and partitions the given array around the
picked pivot. There are many different versions of quick sort that pick
pivot in different ways.
Algorithm:
Always pick first element as pivot.
Always pick last element as pivot (implemented below)
Pick a random element as pivot.
Pick median as pivot.
The key process in quickSort is partition (). Target of partitions is, given
an array and an element x of array as pivot, put x at its correct position
in sorted array and put all smaller elements (smaller than x) before x,
and put all greater elements (greater than x) after x. All this should be
done in linear time.

5. Selection sort

The selection sort algorithm sorts an array by repeatedly finding the


minimum element (considering ascending order) from unsorted part and
putting it at the beginning. The algorithm maintains two subarrays in a
given array.
Algorithm:
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering
ascending order) from the unsorted subarray is picked and moved to the
sorted subarray.
6. Array sort
Sorting an array is the process of arranging all the elements in an
array in a specific order.
Output:

7. Linked list
Since LinkedList implements the java.util.List interface, you
can sort the LinkedList by using Collections.sort () method, just
like you sort an ArrayList. Since LinkedList class implements
the linked list data structure which doesn't provide random
access based upon the index, sorting is quite expensive.
LinkedList each element is connected with next element by
link.So finding n-th element takes n operations. Inserting in any
place takes 1 operation

Output:

8.Trees
Tree sort is a sorting algorithm that is based on Binary
Search Tree data structure. It first creates a binary search
tree from the elements of the input list or array and then
performs an in-order traversal on the created binary
search tree to get the elements in sorted order.
Algorithm:
Step 1: Take the elements input in an array.
Step 2: Create a Binary search tree by inserting data items
from the array into the binary search tree.
Step 3: Perform in-order traversal on the tree to get the
elements in sorted order.

9. Stack
We follow this algorithm:
1.Create a temporary stack.
2.As long as the input stack is NOT empty,
do this:
3.Pop an item from the input stack, call it as
long as the temporary stack is NOT empty
and the top of the temporary stack is larger
than the temporary, pop from the temporary
stack and push it onto the input stack put
temperature on temporary stack
4.Sorted numbers are on the stack

10.Queue
A queue is an abstract data type with a first-
in-first-out element access discipline.
Adding an element is possible only to the
end of the queue, selection - only from the
head of the queue, while the selected
element is removed from the queue.

11.HashSet
The HashSet class implements the Set interface, is based
on a hash table, and is also maintained using a HashMap
instance. In a HashSet, the elements are not ordered, there
is no guarantee that the elements will be in the same order
after some time. The operations of adding, deleting and
searching will be performed in constant time, provided
that the hash function correctly distributes the items into
the "baskets", which will be discussed later.

12.TreeSet
TreeSet is one of the most important implementations of
the SortedSet interface in Java that uses a Tree for
storage. The ordering of the elements is maintained by a
set using their natural ordering whether or not an explicit
comparator is provided. This must be consistent with
equals if it is to correctly implement the Set interface. It
can also be ordered by a Comparator provided at set
creation time, depending on which constructor is used.
The TreeSet implements a NavigableSet interface by
inheriting AbstractSet class.

You might also like