0% found this document useful (0 votes)
36 views9 pages

Merge Final

Uploaded by

shifodryana
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)
36 views9 pages

Merge Final

Uploaded by

shifodryana
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/ 9

‫‪Sorting algorithm‬‬

‫للطالب ‪ :‬الشريف حمد جبريل امراجع(‪)1212044‬‬

‫لمادة ‪data structure:‬‬

‫تحت اشراف‪ :‬أ‪.‬سالم الفرجاني‬

‫الفصل الرابع | ربيع ‪2023‬‬

‫‪1‬‬
Merge sort

‫ على أنه خوارزمية فرز تعمل عن طريق تقسيم مصفوفة إلى مصفوفات فرعية أصغر‬Merge sorting‫يتم تعريف‬
‫ ثم دمج المصفوفات الفرعية التي تم فرزها معا لتشكيل المصفوفة النهائية الذي تم‬، ‫ وفرز كل مصفوفة فرعية‬،
.‫فرزها‬

Java program for Merge Sort //

;*.import java.io

{ class MergeSort

.][Merges two subarrays of arr //

First subarray is arr[l..m] //

Second subarray is arr[m+1..r] //

void merge(int arr[], int l, int m, int r)

Find sizes of two subarrays to be merged //

;int n1 = m - l + 1

;int n2 = r - m

Create temp arrays //

;int L[] = new int[n1]

;int R[] = new int[n2]

Copy data to temp arrays //

for (int i = 0; i < n1; ++i)

;L[i] = arr[l + i]

for (int j = 0; j < n2; ++j)

;R[j] = arr[m + 1 + j]

Merge the temp arrays //

Initial indices of first and second subarrays //

;int i = 0, j = 0

Initial index of merged subarray array //

;int k = l

{ while (i < n1 && j < n2)

{ if (L[i] <= R[j])

;arr[k] = L[i]

;++i

2
}

{ else

;arr[k] = R[j]

;++j

;++k

Copy remaining elements of L[] if any //

{ while (i < n1)

;arr[k] = L[i]

;++i

;++k

Copy remaining elements of R[] if any //

{ while (j < n2)

;arr[k] = R[j]

;++j

;++k

Main function that sorts arr[l..r] using //

)(merge //

void sort(int arr[], int l, int r)

{ if (l < r)

Find the middle point //

;int m = l + (r - l) / 2

Sort first and second halves //

;sort(arr, l, m)

;sort(arr, m + 1, r)

Merge the sorted halves //

;merge(arr, l, m, r)

A utility function to print array of size n //

static void printArray(int arr[])

;int n = arr.length

3
‫)‪for (int i = 0; i < n; ++i‬‬

‫)" " ‪;System.out.print(arr[i] +‬‬

‫‪;)(System.out.println‬‬

‫}‬

‫‪Driver code //‬‬

‫)][‪public static void main(String args‬‬

‫{‬

‫} ‪;int arr[] = { 12, 11, 13, 5, 6, 7‬‬

‫)"‪;System.out.println("Given array is‬‬

‫)‪;printArray(arr‬‬

‫‪;)(MergeSort ob = new MergeSort‬‬

‫)‪;ob.sort(arr, 0, arr.length - 1‬‬

‫)"‪;System.out.println("\nSorted array is‬‬

‫)‪;printArray(arr‬‬

‫}}‬

‫التعقيد الزمني ل ‪ Merge Sort‬هو ()‪ )Nlog (N‬في جميع الحاالت ‪( 3‬األسوأ والمتوسط واألفضل) حيث يقسم ‪Merge Sort‬‬
‫دائما مصفوفة إلى نصفين ويستغرق وقتا خطيا لدمج نصفين‪.‬‬

‫مساحة مساعدة‪ ، O (n) :‬في فرز الدمج ‪ ،‬يتم نسخ جميع العناصر في مصفوفة مساعدة‪ .‬لذلك مطلوب مساحة إضافية ‪N‬‬
‫ل‪.Merge sorting‬‬
‫‪Counting sort‬‬

‫هو أسلوب فرز يعتمد على المفاتيح( القيم الفريدة التي يمكن أن تأخذها العناصر المراد ترتيبها ) بين نطاق‬
‫معين‪ .‬وهو يعمل عن طريق حساب عدد الكائنات التي لها قيم رئيسية مميزة (نوع من التجزئة)‪ .‬ثم قم ببعض‬
‫العمليات الحسابية لحساب موضع كل كائن في تسلسل اإلخراج‪.‬‬

‫يوضح المثال التالي طريقة عمل الخوارزمية‪:‬‬

‫‪.‬لنفترض أّن لدينا المدخالت التالية وهي ضمن النطاق ‪ 1‬إلى ‪9‬‬

‫‪1, 4, 1, 2, 7, 5, 2‬‬

‫ننشئ مصفوفة لتخزين عدد المرات التي يتكرر فيها كل عنصر فريد )‪1‬‬

‫‪Index:‬‬ ‫‪0 1 2 3 4 5 6 7 8 9‬‬


‫‪Count:‬‬ ‫‪0 2 2 0 1 1 0 1 0 0‬‬

‫تعديل مصفوفة العد بطريقة تجعل كل موقع يخّزن مجموع المعدودات السابقة )‪2‬‬

‫‪4‬‬
Index: 0 1 2 3 4 5 6 7 8 9
Count: 0 2 4 4 5 6 6 7 7 7

:‫فيما يلي تنفيذ الخوارزمية‬


// Java implementation of Counting Sort

import java.io.*;

class CountingSort {
void sort(char arr[])
{
int n = arr.length;

// The output character array that will have sorted


// arr
char output[] = new char[n];

// Create a count array to store count of individual


// characters and initialize count array as 0
int count[] = new int[256];
for (int i = 0; i < 256; ++i)
count[i] = 0;

// store count of each character


for (int i = 0; i < n; ++i)
++count[arr[i]];

// Change count[i] so that count[i] now contains


// actual position of this character in output array
for (int i = 1; i <= 255; ++i)
count[i] += count[i - 1];

// Build the output character array


// To make it stable we are operating in reverse
// order.
for (int i = n - 1; i >= 0; i--) {
output[count[arr[i]] - 1] = arr[i];
--count[arr[i]];
}

// Copy the output array to arr, so that arr now


// contains sorted characters
for (int i = 0; i < n; ++i)
arr[i] = output[i];
}

// Driver code
public static void main(String args[])
{
CountingSort ob = new CountingSort();
char arr[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o',
'r', 'g', 'e', 'e', 'k', 's' };

// Function call
ob.sort(arr);

System.out.print("Sorted character array is ");


for (int i = 0; i < arr.length; ++i)
System.out.print(arr[i]); }}
‫الناتج‬

5
Sorted character array is eeeefggkkorss
time complexity
.‫ هو نطاق اإلدخال‬K ‫ هو عدد العناصر في مصفوفة اإلدخال و‬N ‫ حيث‬O (N + K) :
O (N + K) :‫المساحة اإلضافية‬

:‫ لمصفوفة بعناصر سالبة‬Counting sort

:‫لحل المشكلة اتبع الفكرة التالية‬


‫ ألنه ال توجد‬.‫ السابق هي أننا لم نتمكن من فرز العناصر إذا كانت لدينا أعداد سالبة فيها‬Counting sort ‫كانت المشكلة في‬
.‫مؤشرات مصفوفة سلبية‬
‫إذن ما نفعله هو إيجاد الحد األدنى للعنصر وسنخزن عدد هذا العنصر األدنى عند مؤشر الصفر‬
:‫فيما يلي تنفيذ النهج أعاله‬

// Java program for the above approach


import java.util.*;

class GFG {

static void countSort(int[] arr)


{
int max = Arrays.stream(arr).max().getAsInt();
int min = Arrays.stream(arr).min().getAsInt();
int range = max - min + 1;
int count[] = new int[range];
int output[] = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
count[arr[i] - min]++;
}

for (int i = 1; i < count.length; i++) {


count[i] += count[i - 1];
}

for (int i = arr.length - 1; i >= 0; i--) {


output[count[arr[i] - min] - 1] = arr[i];
count[arr[i] - min]--;
}

for (int i = 0; i < arr.length; i++) {


arr[i] = output[i];
}
}

static void printArray(int[] arr)


{
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}

// Driver code
public static void main(String[] args)
{
int[] arr = { -5, -10, 0, -3, 8, 5, -1, 10 };

// Function call
countSort(arr);
printArray(arr);

6
‫}}‬

‫الناتج‬
‫‪10 8 5 0 1- 3- 5- 10-‬‬
‫‪ time complexity‬تعقيد الوقت‪ ، O (N) :‬حيث ‪ N‬هو العدد اإلجمالي للعناصر‬
‫المساحة المساعدة‪O (N) :‬‬

‫‪Qucik sort‬‬

‫‪ QuickSort‬هي خوارزمية فرز تعتمد على خوارزمية ‪ Divide and Conquer‬التي تختار عنصرا‬
‫كمحور وتقسم المصفوفة المحددة حول المحور المختار عن طريق وضع المحور في موضعه‬
‫الصحيح في المصفوفة التي تم فرزها‪.‬‬

‫كيف يعمل الفرز السريع؟‬


‫العملية الرئيسية في ‪ quickSort‬هي قسم ()‪ .‬الهدف من األقسام هو وضع المحور (يمكن اختيار أي عنصر ليكون محوريا)‬
‫في موضعه الصحيح في المصفوفة المرتبة الذي تم فرزه ووضع جميع العناصر األصغر على يسار المحور ‪ ،‬وجميع العناصر‬
‫األكبر على يمين المحور‪.‬‬
‫يتم إجراء هذا القسم بشكل متكرر والذي يقوم أخيرا بفرز المصفوفة‪.‬‬

‫برنامج لتنفيذ الفرز السريع‪:‬‬

‫‪// Java implementation of QuickSort‬‬


‫;*‪import java.io.‬‬

‫{ ‪class GFG‬‬

‫‪// A utility function to swap two elements‬‬


‫)‪static void swap(int[] arr, int i, int j‬‬
‫{‬
‫;]‪int temp = arr[i‬‬
‫;]‪arr[i] = arr[j‬‬
‫;‪arr[j] = temp‬‬

‫‪7‬‬
}

// This function takes last element as pivot,


// places the pivot element at its correct position
// in sorted array, and places all smaller to left
// of pivot and all greater elements to right of pivot
static int partition(int[] arr, int low, int high)
{
// Choosing the pivot
int pivot = arr[high];

// Index of smaller element and indicates


// the right position of pivot found so far
int i = (low - 1);

for (int j = low; j <= high - 1; j++) {

// If current element is smaller than the pivot


if (arr[j] < pivot) {

// Increment index of smaller element


i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return (i + 1);
}

// The main function that implements QuickSort


// arr[] --> Array to be sorted,
// low --> Starting index,
// high --> Ending index
static void quickSort(int[] arr, int low, int high)
{
if (low < high) {

// pi is partitioning index, arr[p]


// is now at right place
int pi = partition(arr, low, high);

// Separately sort elements before


// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
// To print sorted array
public static void printArr(int[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}

8
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 10, 7, 8, 9, 1, 5 };
int N = arr.length;

// Function call
quickSort(arr, 0, N - 1);
System.out.println("Sorted array:");
printArr(arr);
}
}

Output
Sorted array:
1 5 7 8 9 10
:‫تحليل الفرز السريع‬

.‫ على النحو التالي‬، ‫ بشكل عام‬، QuickSort ‫يمكن كتابة الوقت الذي يستغرقه‬
T(n) = T(k) + T(n-k-1) + (n)

RESOURCES & REFERNCES

wikipedia.org 
geeksforgeeks.org 
wiki.hsoub.com 

You might also like