Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Data Structures and Algorithms

Sorting&Search
Dr.Associate Professor
/Abdualmajed Al-Khulaidi
External sorting methods
External sort: They are used when we cannot
place the objects we want to arrange in the
memory. It is used with big data.
Bubble sorting algorithm
Bubble Sort Algorithm: It is an algorithm based
on the idea of repeatedly comparing a pair of
neighboring elements, and switching their
positions if they are in the wrong order.
Bubble sorting algorithm
1- In the first step, we take the first pair of
elements and compare them.
2- In the absence of an arrangement of the
elements, their locations are switched so that
they are arranged.
3- The process continues repeatedly to compare
two pairs of items until all the elements are
perfectly arranged.
Example

7 4 5 2

4 7 5 2
4 5 7 2
4 5 2 7
4 2 5 7
2 4 5 7
Searching

Searching method:
An item is searched for in a group of objects.
Type of searching
Sequential Search:
In this type of search, the elements of the matrix are
navigated in succession, checking each element in it. An
example of this type is Linear Search.
Interval Search:
These algorithms are specially designed to search for data
structures that are in order. These algorithms are more
efficient than linear searches because they target the
middle of the search structure continuously, and filter the
space occupied by the search process, and an example of
this type is Binary Search.
Linear search algorithm
1- The search matrix has an Index, which starts from one
or zero, and does not differ much, as the result is the
same.
2- The search begins with checking the first element,
whether it is equal to the element to be searched for, if
the element to be searched is found, the index number of
the element is preserved and the search process is
terminated.
3- If the element to be searched is not found, the search
will move from the first element in the array to the
second, then the third, and so on until the search for the
element to be searched is completed in the entire
matrix.
‫‪Example‬‬
‫ابحث عن العنصر ‪ x=4‬في المصفوفة التالية باستخدام خوارزمية ‪linear search‬‬

‫تبدأ عملية البحث بمقارنة العنصر بالخانة رقم ‪ 1‬حيث ‪ ،j=1‬هل ‪A[j]=x‬؟ ال‪ .‬إذن ننتقل إلى الخطوة الثانية‪.‬‬ ‫•‬
‫مقارنة العنصر بالخانة رقم ‪ 2‬حيث ‪ ،j=2‬هل ‪A[j]=x‬؟ ال‪ .‬إذن ننتقل إلى الخطوة الثالثة‪.‬‬ ‫•‬
‫مقارنة العنصر بالخانة رقم ‪ 3‬حيث ‪ ،j=3‬هل ‪A[j]=x‬؟ ال‪ .‬إذن ننتقل إلى الخطوة الرابعة‪.‬‬ ‫•‬
‫مقارنة العنصر بالخانة رقم ‪ 4‬حيث ‪ ،j=4‬هل ‪A[j]=x‬؟ ال‪ .‬إذن ننتقل إلى الخطوة الخامسة‪.‬‬ ‫•‬
‫مقارنة العنصر بالخانة رقم ‪ 5‬حيث ‪ ،j=5‬هل ‪A[j]=x‬؟ نعم‪ .‬إذن هنا نحتفظ بالرقم ‪ 5‬و تُعتبر هذه نتيجة البحث‪.‬‬ ‫•‬
Binary Search algorithm

1- Like other algorithms, the required input must be provided, which is the element to be searched for and the matrix
to be searched.
2- The matrix must be in ascending order (from smallest to largest), in case the matrix is ​not ordered in ascending
order, you must arrange it in ascending order in order to be able to apply the binary search algorithm.
3- The search matrix has an Index that starts from zero or one, both of which have the same result.
4- The algorithm begins its work by placing the index pointer in the middle of the matrix until it divides the matrix into
two parts, and here is the beginning of the actual use of the principle of (divide and conquer).
5- You have this question on your mind, "There are matrices with an even number of elements and others with an odd
number of elements, so how can the matrix index be in the two cases in the middle?" I congratulate you for this
thinking. The process of selecting the middle of the matrix is ​simple and easy, all you have to follow the following
formula: Low + high / 2
6- The element is compared with the mean (where the cursor is) with the element to be searched for, and the result is
one of three cases:
- That the value of the element in the array equals the value of the element to be searched for, and thus the value of
the index index is preserved and the search is stopped.
- The value of the element to be searched for is greater than the value of the element where the cursor is, in this case
all the elements of the left half of the array are ignored and the right part of the array becomes a new entry for a
search by the binary search algorithm.
- That the value of the element to be searched for is less than the value of the element where the cursor is, in this case
all the elements of the right half of the array are ignored and the left part of the array becomes a new entry for a
search by the binary search algorithm.
Example

You might also like