0% found this document useful (0 votes)
5 views82 pages

5-Sort Alghrithem 2

Uploaded by

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

5-Sort Alghrithem 2

Uploaded by

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

‫المحاضرة ‪5‬‬

‫خوارزميات الفرز والترتيب‬


‫‪Lecturer: Haitham A Saffour‬‬

‫‪Ma:H.A.S‬‬
Review

What is time complexity?

06/26/2024
Ma:H.A.S
Divide and Conquer

Divide and Conquer is an algorithmic paradigm.


A typical Divide and Conquer algorithm solves a problem using following three steps.

1.Divide: Break the given problem into subproblems of same type.


2.Conquer: Recursively solve these subproblems
3.Combine: Appropriately combine the answers

06/26/2024
Ma:H.A.S
‫ترتيب االختيار‬

‫‪06/26/2024‬‬
‫‪Ma:H.A.S‬‬
Selection sort

• Selection sort is a simple and efficient sorting


algorithm that works by repeatedly selecting
the smallest (or largest) element from the
unsorted portion of the list and moving it to
the sorted portion of the list.

06/26/2024
Ma:H.A.S
Ex:

• Lets consider the following array as an


example: arr[] = {64, 25, 12, 22, 11}

06/26/2024
Ma:H.A.S
First pass:
• For the first position in the sorted array, the whole array is traversed from
index 0 to 4 sequentially. The first position where 64 is stored presently,
after traversing whole array it is clear that 11 is the lowest value.
• Thus, replace 64 with 11. After one iteration 11, which happens to be the
least value in the array, tends to appear in the first position of the sorted
list.

06/26/2024
Ma:H.A.S
Second Pass:

• For the second position, where 25 is present, again traverse the rest of the
array in a sequential manner.
• After traversing, we found that 12 is the second lowest value in the array
and it should appear at the second place in the array, thus swap these
values.

06/26/2024
Ma:H.A.S
Third Pass:

• Now, for third place, where 25 is present again traverse the rest of the
array and find the third least value present in the array.
• While traversing, 22 came out to be the third least value and it should
appear at the third place in the array, thus swap 22 with element present
at third position.

06/26/2024
Ma:H.A.S
Fourth pass:

• Similarly, for fourth position traverse the rest of the array and find the
fourth least element in the array
• As 25 is the 4th lowest value hence, it will place at the fourth position.

06/26/2024
Ma:H.A.S
Fifth Pass:

• At last the largest value present in the array automatically get placed at
the last position in the array
• The resulted array is the sorted array.

06/26/2024
Ma:H.A.S
Code
• // C++ program for implementation of • // Function to print an array
• // selection sort • void printArray(int arr[], int size)
• #include <bits/stdc++.h> • {
• using namespace std; • int i;
• for (i = 0; i < size; i++) {
• // Function for Selection sort • cout << arr[i] << " ";
• void selectionSort(int arr[], int n)
• cout << endl;
• {
• }
• int i, j, min_idx;
• }
• // One by one move boundary of
• // unsorted subarray
• // Driver program
• for (i = 0; i < n - 1; i++) { • int main()
• {
• // Find the minimum element in • int arr[] = { 64, 25, 12, 22, 11 };
• // unsorted array • int n = sizeof(arr) / sizeof(arr[0]);
• min_idx = i;
• for (j = i + 1; j < n; j++) { • // Function Call
• if (arr[j] < arr[min_idx]) • selectionSort(arr, n);
• min_idx = j; • cout << "Sorted array: \n";
• } • printArray(arr, n);
• return 0;
• // Swap the found minimum element • }
• // with the first element
• if (min_idx != i)
• // This is code is contributed by rathbhupendra
• swap(arr[min_idx], arr[i]);
06/26/2024
• }
Ma:H.A.S
‫الكود البرمجي‬
• // Selection Sort
• int a[] = {100,35,500,9,67,20};
• int n=6;
• int min_index;
• for (int i=0; i <n-1; i++)
• {
• min_index = i ;
• for (int j = i ; j <n ; j++)
• if (a[j] < a [min_index])
• min_index = j;
• swap (a[i] , a[min_index]);


• }
• printArray(a, n);
• return 0;
• }
06/26/2024
Ma:H.A.S
‫شرح الكود‬

‫‪06/26/2024‬‬
‫‪Ma:H.A.S‬‬
Complexity Analysis of Selection Sort

• Time Complexity: The time complexity of


Selection Sort is O(N2) as there are two nested
loops:
• One loop to select an element of Array one by
one = O(N)
• Another loop to compare that element with
every other Array element = O(N)
• Therefore overall complexity = O(N) * O(N) =
O(N*N) = O(N2)
06/26/2024
Ma:H.A.S
Advantages vs DisAdvantages

• Advantages
• Simple and easy to understand.
• Works well with small datasets.
• Disadvantages
• Selection sort has a time complexity of O(n^2) in
the worst and average case.
• Does not work well on large datasets.
• Does not preserve the relative order of items with
equal keys which means it is not stable.
06/26/2024
Ma:H.A.S
Insertion Sort

06/26/2024
Ma:H.A.S
Insertion Sort

• insertion sort is a simple sorting algorithm that works


similar to the way you sort playing cards in your hands. The
array is virtually split into a sorted and an unsorted part.
Values from the unsorted part are picked and placed at the
correct position in the sorted part.
• Characteristics of Insertion Sort:
• This algorithm is one of the simplest algorithm with simple
implementation
• Basically, Insertion sort is efficient for small data values
• Insertion sort is adaptive in nature, i.e. it is appropriate for
data sets which are already partially sorted.
06/26/2024
Ma:H.A.S
First Pass:

• Consider an example: arr[]: {12, 11, 13, 5, 6}


• initially, the first two elements of the array are compared in
insertion sort.
• 12 11 13 5 6

• Here, 12 is greater than 11 hence they are not in the


ascending order and 12 is not at its correct position. Thus,
swap 11 and 12.
• So, for now 11 is stored in a sorted sub-array.

• 11 12 13 5 6
06/26/2024
Ma:H.A.S
Second Pass:

• Now, move to the next two elements and


compare them

• 11 12 13 5 6

• Here, 13 is greater than 12, thus both


elements seems to be in ascending order,
hence, no swapping will occur. 12 also stored in
a sorted sub-array along with 11
06/26/2024
Ma:H.A.S
Third Pass:
• Now, two elements are present in the sorted sub-array which are 11 and 12
• Moving forward to the next two elements which are 13 and 5

• 11 12 13 5 6

• Both 5 and 13 are not present at their correct place so swap them

• 11 12 5 13 6

• After swapping, elements 12 and 5 are not sorted, thus swap again

• 11 5 12 13 6

• Here, again 11 and 5 are not sorted, hence swap again

• 5 11 12 13 6
06/26/2024
Ma:H.A.S
Fourth Pass:
• Now, the elements which are present in the sorted sub-array are 5, 11 and 12
• Moving to the next two elements 13 and 6

• 5 11 12 13 6

• Clearly, they are not sorted, thus perform swap between both

• 5 11 12 6 13

• Now, 6 is smaller than 12, hence, swap again

• 5 11 6 12 13

• Here, also swapping makes 11 and 6 unsorted hence, swap again

• 5 6 11 12 13
06/26/2024
Ma:H.A.S
Illustrations:

06/26/2024
Ma:H.A.S
Pseudo Code of Insertion Sort

• procedure insertionSort(arr):
• for i = 1 to n-1
• key = arr[i]
• j = i-1
• while j >= 0 and arr[j] > key
• swap arr[j+1] with arr[j]
• j=j-1
• end while
• end for
• end function
06/26/2024
Ma:H.A.S
Insertion Sort Algorithm – Iterative Approach

• To sort an array of size N in ascending order:

• Iterate from arr[1] to arr[N] over the array.


• Compare the current element (key) to its predecessor.
• If the key element is smaller than its predecessor,
compare it to the elements before. Move the greater
elements one position up to make space for the swapped
element.

06/26/2024
Ma:H.A.S
Insertion Sort Algorithm – Iterative Approach

• // C++ program for insertion sort • // A utility function to print an array


• // of size n
• #include <bits/stdc++.h>
• void printArray(int arr[], int n)
• using namespace std;
• {
• // Function to sort an array using • int i;
• // insertion sort • for (i = 0; i < n; i++)
• void insertionSort(int arr[], int n) • cout << arr[i] << " ";
• { • cout << endl;
• int i, key, j;
• }
• for (i = 1; i < n; i++)
• {
• key = arr[i]; • // Driver code
• j = i - 1; • int main()
• {
• // Move elements of arr[0..i-1],
• int arr[] = { 12, 11, 13, 5, 6 };
• // that are greater than key, to one
• // position ahead of their • int N = sizeof(arr) / sizeof(arr[0]);
• // current position
• while (j >= 0 && arr[j] > key) • insertionSort(arr, N);
• { • printArray(arr, N);
• arr[j + 1] = arr[j];
• j = j - 1;
• } • return 0;
•06/26/2024 arr[j + 1] = key; • }
• } • Ma:H.A.S
// This is code is contributed by rathbhupendra
Insertion sort algorithm – Recursive Approach

• Starting from the second element, traverse through the input


array from left to right.
• For each element, compare it with the elements in the sorted
subarray to its left, starting from the rightmost element.
• If an element in the sorted subarray is greater than the
current element, shift that element one position to the right.
• Repeat step 3 until you find an element that is less than or
equal to the current element.
• Insert the current element into the position immediately to
the right of the element found in step 4.
• Repeat steps 2-5 for all remaining elements in the unsorted
subarray.

06/26/2024
Ma:H.A.S
Code in c lang
• #include <iostream>
• #include <vector> • void printArray(vector<int>& arr, int n)
• using namespace std;
• {
• void recursiveInsertionSort(vector<int>& arr, int n) • for (int i = 0; i < n; i++)
• {
• {
• // Base case: if the array has only one element, it is already
sorted • cout << arr[i] << " ";
• if (n <= 1) { • }
• return;
• } • cout << endl;
• // Sort the first n-1 elements of the array recursively • }
• recursiveInsertionSort(arr, n - 1);

• // Insert the nth element into its correct position in the sorted • int main()
subarray
• {
• int last = arr[n - 1];
• int j = n - 2; • vector<int> arr = {12, 11, 13, 5, 6};

• int n = arr.size();
• // Shift elements to the right to make space for the nth element
• while (j >= 0 && arr[j] > last)
• { • recursiveInsertionSort(arr, n);
• arr[j + 1] = arr[j];
• printArray(arr, n);
• j--;
• }

• return 0;
• // Place the nth element in its correct position
06/26/2024 • }
• arr[j + 1] = last;
Ma:H.A.S
• }
06/26/2024
Ma:H.A.S
Insertion Sort ‫ترتيب اإلدراج‬
Consider the following observations:
– A list with one element is sorted
– In general, if we have a sorted list of k -1 items, we
can insert a new item to create a sorted list of size k
For example, consider this sorted array containing of eight
sorted entries
5 7 12 19 21 26 33 40 14 9 18 21 2

Suppose we want to insert 14 into this array leaving the


resulting array sorted
( linked list ( ‫وهي خيار جيد بالتعامل مع القوائم المرتبطة‬

Ma:H.A.S
Insertion Sort ‫ترتيب اإلدراج‬.

Starting at the back, if the number is greater than 14,


copy it to the right
- Once an entry less than 14 is found, insert 14 into the
resulting vacancy

Ma:H.A.S
06/26/2024
Ma:H.A.S
Insertion Sort ‫ترتيب اإلدراج‬.

Code for insert item in correct position


would be:
for ( int j = k; j > 0; --j ) {
if ( array[j - 1] > array[j] ) {
swap( array[j - 1], array[j] );
} else {
// As soon as we don't need to swap, the just
// is in the correct location
break;
}
}

Ma:H.A.S
Insertion Sort Algorithm

void insertion_sort( Type *const array, int const n )


{
for ( int k = 1; k < n; ++k )
{
for ( int j = k; j > 0; --j )
{
if ( array[j - 1] > array[j] )
{
swap( array[j - 1], array[j] );
}
else
break;
}
}
}
Ma:H.A.S
Insertion Sort Algorithm analysis
and complexity time

n 1 n  n  1
Thus, the worst-case run time is k   O n2 
k 1 2

Ma:H.A.S
Insertion

06/26/2024
Ma:H.A.S
Insertion VS selection

06/26/2024
Ma:H.A.S
Bubble Sort

• Bubble Sort is the simplest sorting algorithm that works by repeatedly


swapping the adjacent elements if they are in the wrong order. This
algorithm is not suitable for large data sets as its average and worst-case
time complexity is quite high.
• Bubble Sort Algorithm

• In this algorithm,

• traverse from left and compare adjacent elements and the higher one
is placed at right side.
• In this way, the largest element is moved to the rightmost end at first.
• This process is then continued to find the second largest and place it
and so on until the data is sorted.

06/26/2024
Ma:H.A.S
How does Bubble Sort Work?
First Pass:

• Input: arr[] = {6, 3, 0, 5}

06/26/2024
Ma:H.A.S
Second Pass:
Place the second largest element at correct
position

06/26/2024
Ma:H.A.S
Third Pass:

Place the remaining two elements


at their correct positions.

06/26/2024
Ma:H.A.S
Implementation of Bubble Sort

• // Optimized implementation of Bubble sort


• #include <bits/stdc++.h> • // Function to print an array
• using namespace std; • void printArray(int arr[], int size)
• {
• // An optimized version of Bubble Sort

• int i;
void bubbleSort(int arr[], int n)
• { • for (i = 0; i < size; i++)
• int i, j; • cout << " " << arr[i];
• bool swapped; • }
• for (i = 0; i < n - 1; i++) {
• swapped = false;
• for (j = 0; j < n - i - 1; j++) { • // Driver program to test above functions
• if (arr[j] > arr[j + 1]) { • int main()
• swap(arr[j], arr[j + 1]); • {
• swapped = true; • int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
• }

• int N = sizeof(arr) / sizeof(arr[0]);
}
• bubbleSort(arr, N);
• // If no two elements were swapped • cout << "Sorted array: \n";
• // by inner loop, then break • printArray(arr, N);
• if (swapped == false)
• return 0;
• break;
• } • }
• } • // This code is contributed by shivanisinghss2110
06/26/2024
Ma:H.A.S
Advantages vs Dis Advantages

• Advantages of Bubble Sort:

• Bubble sort is easy to understand and implement.


• It does not require any additional memory space.
• It is a stable sorting algorithm, meaning that elements with the same key
value maintain their relative order in the sorted output.

• Disadvantages of Bubble Sort:

• Bubble sort has a time complexity of O(N2) which makes it very slow for
large data sets.
• Bubble sort is a comparison-based sorting algorithm, which means that it
requires a comparison operator to determine the relative order of elements in
the input data set. It can limit the efficiency of the algorithm in certain cases.

06/26/2024
Ma:H.A.S
‫الترتيب الفقاعي‬

‫‪06/26/2024‬‬
‫‪Ma:H.A.S‬‬
‫متابعة‬

‫• تقارن هذه الخوارزمية بين قيم الخانات المتجاورة‪ ،‬تبدأ بالمقارنة بين أول خانتين من‬
‫المصفوفة‪ ،‬إذا كان محتوى الخانة األولى أكبر من محتوى الخانة الثانية سيتم تبادل‬
‫محتوى الخانتين و هكذا مع بقية الخانات‪.‬‬
‫• عند انتهاء الدورة األولى ستكون الخوارزمية قد‬
‫أنجزت ‪ n-1‬مقارنة و بهذا ستتم إزاحة أكبر عناصر المصفوفة إلى الخانة األخيرة‪.‬‬
‫بقي اآلن ترتيب الــ ‪n-1‬عنصر‪ .‬و هكذا األمر مع بقية الدورات ‪..‬‬

‫النتيجة ‪ :‬عملية الترتيب تـخـتلف عن عملية البحث في هذه النقطة‪ ،‬فالترتيب ال يقبل‬ ‫•‬
‫وجود عدة احتماالت في النتيجة‪ ،‬فعند تطبيق الخوارزمية سنحصل على مصفوفة مرتبة‬
‫تصاعديا و بالتالي ال توجد احتماالت للمناقشة‪.‬‬
‫•‬
‫اإليجابيات ‪ :‬الخوارزمية سهلة التصور و بسيطة المفهوم‪.‬‬
‫السلبيات ‪ :‬بطيئة شيئا ما وغير عملية‪ ،‬خصوصا عند معالجة المصفوفات الضخمة‬

‫‪06/26/2024‬‬
‫‪Ma:H.A.S‬‬
‫الكود البرمجي‬
• #include "stdafx.h"
• #include <iostream>
• #include<string>
• using namespace std;

• int _tmain(int argc, _TCHAR* argv[])


• { Just for print array:
• //Bubble Sort
• int a[]= {9,6,5,3,2,1};
• int n=6; void printArray(int a[], int n)
• for ( int k = 0; k < n; k++ ) {
• { for (int i = 0; i < n; i++)
• for ( int j =0 ; j< n-1;j++ )
• {
{
• if ( a[j + 1] <a[j] ) cout << a[i] << " ";
• { }
• swap( a[j + 1], a[j] );
}
• }
• }
• }
• printArray(a, n);
• return 0;
• }

06/26/2024
Ma:H.A.S
‫التعقيد الزمني‬

‫‪06/26/2024‬‬
‫‪Ma:H.A.S‬‬
‫مثال‬

06/26/2024
Ma:H.A.S
Quick Sort

• Quick Sort is a sorting algorithm based on the


Divide and Conquer algorithm that picks an
element as a pivot and partitions the given
array around the picked pivot by placing the
pivot in its correct position in the sorted array.

06/26/2024
Ma:H.A.S
How does Quick Sort work?
• The key process in quick Sort is a partition(). The target of partitions is to
place the pivot (any element can be chosen to be a pivot) at its correct
position in the sorted array and put all smaller elements to the left of the
pivot, and all greater elements to the right of the pivot.
• Partition is done recursively on each side of the pivot after the pivot is
placed in its correct position and this finally sorts the array.

06/26/2024
Ma:H.A.S
Step 1
• Consider: arr[] = {10, 80, 30, 90, 40}.
• Compare 10 with the pivot and as it is less than pivot arrange it
accrodingly.

06/26/2024
Ma:H.A.S
Step 2

Compare 80 with the pivot. It is greater than pivot.

06/26/2024
Ma:H.A.S
Step 3

• Compare 30 with pivot. It is less than pivot so


arrange it accordingly.

06/26/2024
Ma:H.A.S
Step 4

• Compare 90 with the pivot. It is greater than


the pivot.

06/26/2024
Ma:H.A.S
Step 5

• Arrange the pivot in its correct position.

06/26/2024
Ma:H.A.S
Illustration of Quick sort:

06/26/2024
Ma:H.A.S
Code implementation of the Quick Sort:

• // C++ code to implement quicksort


• // The main function that implements QuickSort
• #include <bits/stdc++.h> • // arr[] --> Array to be sorted,
• using namespace std;
• // low --> Starting index,
• // high --> Ending index
• void quickSort(int arr[], int low, int high)
• // This function takes last element as pivot,
• {
• // places the pivot element at its correct position
• if (low < high) {
• // in sorted array, and places all smaller to left
• // of pivot and all greater elements to right of pivot • // pi is partitioning index, arr[p]
• int partition(int arr[], int low, int high) • // is now at right place
• { • int pi = partition(arr, low, high);
• // Choosing the pivot
• int pivot = arr[high]; • // Separately sort elements before
• // partition and after partition
• // Index of smaller element and indicates • quickSort(arr, low, pi - 1);
• // the right position of pivot found so far • quickSort(arr, pi + 1, high);
• int i = (low - 1); • }
• }
• for (int j = low; j <= high - 1; j++) {
• // Driver Code
• // If current element is smaller than the pivot • int main()
• if (arr[j] < pivot) { • {
• int arr[] = { 10, 7, 8, 9, 1, 5 };
• // Increment index of smaller element • int N = sizeof(arr) / sizeof(arr[0]);
• i++;

• // Function call
swap(arr[i], arr[j]);
• quickSort(arr, 0, N - 1);
• }
• cout << "Sorted array: " << endl;
• }
• for (int i = 0; i < N; i++)
• swap(arr[i + 1], arr[high]);
• cout << arr[i] << " ";
• return (i + 1); • return 0;
• }
06/26/2024 • }
Ma:H.A.S
Complexity Analysis of Quick Sort:

• Time Complexity:

• Best Case: \omega (N * log N)


• Average Case: \Theta (N * logN)
• Worst Case: O(N2)

• Space: O(1) as no extra space is used


• Advantages of Quick Sort:

• It is a divide-and-conquer algorithm that makes it easier to solve problems.


• It is efficient on large data sets.
• It has a low overhead, as it only requires a small amount of memory to function.

• Disadvantages of Quick Sort:

• It has a worst-case time complexity of O(N2), which occurs when the pivot is chosen poorly.
• It is not a good choice for small data sets.
• It is not a stable sort, meaning that if two elements have the same key, their relative order will not be
preserved in the sorted output in case of quick sort, because here we are swapping elements according
to the pivot’s position (without considering their original positions).
06/26/2024
Ma:H.A.S
Merge sort ‫ترتيب الدمج‬
.‫خوارزمية دمج الفرز هي خوارزمية فرز تعتمد على نموذج فرق تسد‬ •
.‫ يتم تقسيم المصفوفة مبدئًيا إلى نصفين متساويين ثم يتم دمجهما بطريقة مرتبة‬، ‫في هذه الخوارزمية‬ •
‫ فكر في األمر على أنه خوارزمية متكررة تقسم المصفوفة باستمرار إلى نصفين حتى ال يمكن‬:‫دمج عملية الفرز‬ •
‫ فإن القسمة‬، ‫ هذا يعني أنه إذا أصبحت المصفوفة فارغة أو لم يتبق منها سوى عنصر واحد‬.‫تقسيمها بشكل أكبر‬
.‫ أي أنها الحالة األساسية إليقاف العودية‬، ‫ستتوقف‬
‫ فقم بتقسيمها إلى نصفين واستدعاء ترتيب الدمج بشكل متكرر على‬، ‫إذا كانت المصفوفة تحتوي على عناصر متعددة‬ •
‫ عملية الدمج هي عملية أخذ مصفوفتين‬.‫ يتم تطبيق عملية الدمج‬، ‫ عند فرز كال النصفين‬، ‫ أخيًر ا‬.‫كل من النصفين‬
.‫صغيرتين مرتبة ودمجهما في النهاية إلنشاء مصفوفة أكبر وبالتالي تتطلب احيانا الى مصفوفة اضافية مساعدة‬

The Merge Sort algorithm is a sorting algorithm that is based on the Divide and Conquer paradigm. In this
algorithm, the array is initially divided into two equal halves and then they are combined in a sorted
manner.
Merge Sort Working Process:
Think of it as a recursive algorithm continuously splits the array in half until it cannot be further divided.
This means that if the array becomes empty or has only one element left, the dividing will stop, i.e. it is
the base case to stop the recursion. If the array has multiple elements, split the array into halves and
recursively invoke the merge sort on each of the halves. Finally, when both halves are sorted, the merge
operation is applied. Merge operation is the process of taking two smaller sorted arrays and combining
them to eventually make a larger one.
06/26/2024
Ma:H.A.S
Merge Sort Algorithm

• Merge sort is defined as a sorting algorithm that works by dividing an array into smaller
subarrays, sorting each subarray, and then merging the sorted subarrays back together to form
the final sorted array.

• In simple terms, we can say that the process of merge sort is to divide the array into two
halves, sort each half, and then merge the sorted halves back together. This process is
repeated until the entire array is sorted.
• Need for Merge Sort

• One thing that you might wonder is what is the specialty of this algorithm. We already have a
number of sorting algorithms then why do we need this algorithm? One of the main
advantages of merge sort is that it has a time complexity of O(n log n), which means it can sort
large arrays relatively quickly. It is also a stable sort, which means that the order of elements
with equal values is preserved during the sort.

• Merge sort is a popular choice for sorting large datasets because it is relatively efficient and
easy to implement. It is often used in conjunction with other algorithms, such as quicksort, to
improve the overall performance of a sorting routine.

06/26/2024
Ma:H.A.S
Advantages of Merge Sort:
• Stability: Merge sort is a stable sorting algorithm, which means it maintains the relative
order of equal elements in the input array. This makes it useful in applications where
preserving the original order of equal elements is important.
• Guaranteed worst-case performance: Merge sort has a worst-case time complexity of O(n
log n), which means it performs well even on large datasets. Other sorting algorithms, such
as quicksort, have a worst-case time complexity of O(n^2), which can result in poor
performance on large datasets.
• Parallelizable: Merge sort is a naturally parallelizable algorithm, which means it can be
easily parallelized to take advantage of multiple processors or threads. This makes it useful
for high-performance computing applications.
• Memory efficient: Merge sort has a space complexity of O(n), which means it can sort
datasets that are larger than the available memory on a machine. This makes it useful for
applications where memory usage is a concern.
• Versatility: Merge sort can be used to sort a wide range of data types, including integers,
floating-point numbers, and strings.
• Adaptability: Merge sort can be adapted to handle different input distributions, such as
partially sorted, nearly sorted, or completely unsorted data. This makes it useful in a variety
of real-world applications.
06/26/2024
Ma:H.A.S
illustrator

06/26/2024
Ma:H.A.S
Merge Sort
Divide and conquer
Big Problem

Sub- Sub-
Problem Proble

Sub- Sub- Sub- Sub-


Problem Problem Problem Problem

Sub- Sub- Sub- Sub- Sub- Sub- Sub- Sub-


Problem Problem Problem Problem Problem Problem Problem Problem

Sub -soluation Sub -soluation Sub -soluation Sub -soluation

soluation

06/26/2024
Ma:H.A.S
‫كيف تعمل خوارزمية الدمج؟‬
‫نفحص إذا الفهرس اليساري أقل من اليميني‬ ‫•‬

‫اآلن‪ ،‬يقسم فرز الدمج أوًال المصفوفة بأكملها بشكل تكراري إلى نصفين‬ ‫•‬
‫متساويين ‪ ،‬ما لم تتحقق القيم الذرية‪ .‬هنا ‪ ،‬نرى أن مصفوفة مكونة من ‪7‬‬
‫عناصر مقسمة إلى مصفوفتين بحجم ‪ 4‬و ‪ 3‬على التوالي‪.‬‬

‫اآلن ‪ ،‬مرة ثانية ‪ ،‬اكتشف أن المؤشر األيسر أقل من الفهرس األيمن لكال‬ ‫•‬
‫المصفوفتين ‪ ،‬إذا وجدت نعم ‪ ،‬فقم بحساب النقاط الوسطى لكال المصفوفتين‬
‫مرة أخرى‪.‬‬

‫اآلن ‪ ،‬قّسم هاتين المصفوفتين إلى نصفين إضافيين ‪ ،‬حتى يتم الوصول إلى‬ ‫•‬
‫الوحدات الذرية للمصفوفة وال يمكن إجراء مزيد من التقسيم‪.‬‬

‫‪06/26/2024‬‬
‫‪Ma:H.A.S‬‬
‫متابعة‬

‫بعد تقسيم المصفوفة إلى وحدات أصغر ‪ ،‬ابدأ بدمج العناصر‬


‫مرة أخرى بناًء على مقارنة حجم العناصر أوًال ‪ ،‬قارن بين‬
‫عنصر كل قائمة ثم ادمجهم في قائمة أخرى بطريقة مرتبة‪.‬‬

‫بعد الدمج النهائي ‪ ،‬تبدو القائمة كما يلي‪:‬‬

‫‪06/26/2024‬‬
‫‪Ma:H.A.S‬‬
EX:

06/26/2024
Ma:H.A.S
‫تعقيد خوارزمية ‪Merge‬‬

‫•‬
‫•‬ ‫)‪T(n)=n log(n‬‬ ‫تعقيد الخوارزمية‬

‫‪06/26/2024‬‬
‫‪Ma:H.A.S‬‬
Shell ‫خوارزمية‬

06/26/2024
Ma:H.A.S
06/26/2024
Ma:H.A.S
Con’t

06/26/2024
Ma:H.A.S
Con’t

06/26/2024
Ma:H.A.S
‫خطوات الخوارزمية‬

‫يعتبر ترتيب ‪ Shell‬بشكل أساسي نوًع ا مختلًفا من فرز اإلدراج‪.‬‬ ‫•‬


‫في فرز اإلدراج ‪ ،‬ننقل العناصر إلى موضع واحد فقط لألمام‪ .‬عندما يتعين تحريك عنصر إلى األمام بعيًد ا ‪ ،‬يتم تضمين‬ ‫•‬
‫العديد من الحركات‪.‬‬
‫فكرة ‪ ShellSort‬هي السماح بتبادل العناصر البعيدة‪.‬‬ ‫•‬
‫في ‪ ، Shell Sort‬نجعل المصفوفة ‪ h‬مرتبة لقيمة كبيرة‪ .‬نستمر في تقليل قيمة ‪ h‬حتى تصبح ‪1‬اي عنصر واحد‬ ‫•‬
‫وبالتالي المصفوفة يتم فرزها إذا تم فرز جميع القوائم الفرعية لكل المصفوفة‬ ‫•‬

‫الخطوات‪:‬‬ ‫•‬
‫‪ -1‬ابدأ الخطوة‬ ‫•‬
‫‪ - 2‬تهيئة قيمة حجم الفجوة‪ .‬مثال‪ h :‬الخطوة‬ ‫•‬
‫‪ - 3‬قّسم القائمة إلى جزء فرعي أصغر‪ .‬يجب أن يكون لكل منها فترات متساوية لـ ‪h‬‬ ‫•‬
‫‪ - 4‬قم بفرز هذه القوائم الفرعية باستخدام فرز اإلدراج‬ ‫•‬
‫‪ - 5‬كرر هذه الخطوة ‪ 2‬حتى يتم فرز القائمة‪.‬‬ ‫•‬
‫‪ - 6‬اطبع قائمة مرتبة‪.‬‬ ‫•‬
‫‪ - 7‬توقف‪.‬‬ ‫•‬
‫‪06/26/2024‬‬
‫‪Ma:H.A.S‬‬
‫متابعة‬
‫هذه الخوارزمية فعالة جًد ا لمجموعات البيانات متوسطة الحجم‬ ‫•‬
‫حيث أن متوسط درجة تعقيدها وأسوأ حالة لهذه الخوارزمية يعتمد‬
‫على تسلسل الفجوة األكثر شهرة هو )‪ ، Ο (n‬حيث ‪ n‬هو عدد‬
‫العناصر‪ .‬وأسوأ حالة تعقيد فضاء هو )‪ .O (n‬كيف يعمل ‪Shell‬‬
‫‪ Sort‬؟ دعونا نفكر في المثال التالي للحصول على فكرة عن كيفية‬
‫عمل ترتيب الصدفة‪ .‬نأخذ نفس المصفوفة التي استخدمناها في‬
‫األمثلة السابقة‪ .‬للحصول على مثالنا وسهولة الفهم ‪ ،‬نأخذ الفاصل‬
‫الزمني ‪ .4‬قم بعمل قائمة فرعية افتراضية لجميع القيم الموجودة‬
‫في الفاصل الزمني لـ ‪ 4‬وظائف‪ .‬هذه القيم هي {‪، 33{ ، }14 ، 35‬‬
‫‪ }27 ، 42{ ، }19‬و {‪}44 ، 10‬‬

‫نقارن القيم في كل قائمة فرعية وتبديلها (إذا لزم األمر) في‬ ‫•‬
‫المصفوفة األصلية‪ .‬بعد هذه الخطوة ‪ ،‬يجب أن تبدو المصفوفة‬
‫الجديدة هكذا ‪-‬‬

‫‪06/26/2024‬‬
‫‪Ma:H.A.S‬‬
‫متابعة‬
‫• ثم نأخذ الفترة الزمنية ‪ 1‬وتولد هذه الفجوة قائمتين فرعيتين‬
‫‪}44 ، 33 ، 10 ، 19{ ، }42 ، 35 ، 27 ، 14{ -‬‬

‫• نقوم بمقارنة القيم وتبديلها ‪ ،‬إذا لزم األمر ‪ ،‬في المصفوفة‬


‫األصلية‪ .‬بعد هذه الخطوة ‪ ،‬يجب أن تبدو المصفوفة كما‬
‫يلي‪:‬‬

‫• أخيًر ا ‪ ،‬نقوم بفرز بقية المصفوفة باستخدام فاصل من‬


‫القيمة ‪ .1‬يستخدم نوع ‪ Shell Sort‬فرز اإلدراج لفرز‬
‫المصفوفة‪ .‬فيما يلي التصوير خطوة بخطوة‬
‫‪06/26/2024‬‬
‫‪Ma:H.A.S‬‬
06/26/2024
Ma:H.A.S
Shell Sort Algorithm-Code

void shellSort (int A[], int n)


{ // Rearrange elements at each n/2, n/4,
n/8, ... gaps
for (int gap = n / 2; gap > 0; gap /= 2)
for (int j = gap; j < n; j ++)
{
for (int i = j-gap; i>=0; i -= gap)
if A[i+gap]<A[i])
swap(A[i+gap] , A[i]);
else
break;
}
}
Ma:H.A.S
Shell Sort

• Shell sort is an algorithm that first sorts the elements far apart from
each other and successively reduces the interval between the
elements to be sorted. It is a generalized version of insertion sort.
• In shell sort, elements at a specific interval (gaps) are sorted. The
interval between the elements is gradually decreased based on the
sequence used. The performance of the shell sort depends on the
type of sequence used for a given input array.
• Shell's original sequence: N/2 , N/4 , …, 1

Ma:H.A.S
06/26/2024
Ma:H.A.S
Shell Sort Algorithm analysis
‫وتعقيد الخوارزمية‬

• Worst Case Complexity: less than or equal to O(n2)


• Best Case Complexity: O(n*log n)
When the array is already sorted, the total number
of comparisons for each interval (or increment) is
equal to the size of the array.
• Average Case Complexity: O(n*log n)

n/2 + n/4 + n/8 + n/16 + .... + n/n = n(1/2 + 1/4 + 1/8 +


1/16 + ... + 1/n) = nlogn

Ma:H.A.S
‫‪palindrome‬‬

‫هي سلسلة حرفية تكون متناظرة من اليمين أو اليسار •‬


‫• مثال‪Dad ,Madam :‬‬

‫‪06/26/2024‬‬
‫‪Ma:H.A.S‬‬
Algorithms Complexity

Time Time Time


Sorting Space
Complexity - Complexity - Complexity -
Algorithm Complexity
Best Worst Average
Bubble Sort n n2 n2 1
Selection Sort n2 n2 n2 1
Insertion Sort n n2 n2 1
Merge Sort nlog n nlog n nlog n n
Quicksort nlog n n2 nlog n log n
Counting Sort n+k n+k n+k max
Radix Sort n+k n+k n+k max
Bucket Sort n+k n2 n n+k
Heap Sort nlog n nlog n nlog n 1
Shell Sort nlog n n2 nlog n 1

06/26/2024
Ma:H.A.S
Exam

06/26/2024
Ma:H.A.S

You might also like