0% found this document useful (0 votes)
6 views3 pages

текст

The document outlines various sorting algorithms including Insertion Sort, Selection Sort, Merge Sort, Shell Sort, and Quick Sort. Each algorithm is described with its respective pseudocode, detailing the steps involved in sorting an array. The document serves as a reference for understanding the implementation of these sorting techniques.

Uploaded by

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

текст

The document outlines various sorting algorithms including Insertion Sort, Selection Sort, Merge Sort, Shell Sort, and Quick Sort. Each algorithm is described with its respective pseudocode, detailing the steps involved in sorting an array. The document serves as a reference for understanding the implementation of these sorting techniques.

Uploaded by

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

insertion sort

Int[] a={5,1,6,2,4,3}
Int temp,j;
For(int i=1;i<a.length;it+)
Temp=a[i];
J=i;
While(j>0 && a[j-1]>temp)
A[j]=a[j-1];
J=j-1;
}
A[jl=temp
}
Print all the elements

Selection Sort:

Declare array with list of elements


Int min, temp=0;
for(i=0;i<a.length;i++)
int min=i;
for(j=i+1;j<a.length;j++)
if(a[j)<a[min])
min=j;
}
temp=a(i];
a[i]=a[min];
a [min)=temp;
}

Merge Sort:

Mergesort(A)
{
N = length(A)
if (N <= 1)
Return

Mid = N / 2
Left = array of size (0 to mid - 1)
Right = array of size (mid to N - 1)

for (i = 0 to mid - 1)
Left[i] = A[i]

for (i = mid to N - 1)
Right[i - mid] = A[i]

Mergesort(Left)
Mergesort(Right)

Merge(Left, Right, A)
}

Merge(L, R, A)
{
NL = length(L)
NR = length(R)
i = j = k = 0

While (i < NL && j < NR)


{
if (L[i] <= R[j])
{
A[k] = L[i]
i = i + 1
}
else
{
A[k] = R[j]
j = j + 1
}
k = k + 1
}

While (i < NL)


{
A[k] = L[i]
i = i + 1
k = k + 1
}

While (j < NR)


{
A[k] = R[j]
j = j + 1
k = k + 1
}
}

She’ll Sort:

ShellSort(arr,n)
for (int gap = n / 2; gap > 0; gap /= 2) (
for (int i = gap; i< n; i++) 1
Int temp = arr|);
int j;
for (j= i; j >= gap && arrÜ - gap] > temp; j -= gap) (
arrU) = amli- gap);
arrü) = temp;
}
}
}

Quick Sort:

Quicksort(l, h)
{
if (l < h)
{
j = partition(l, h);
Quicksort(l, j);
Quicksort(j + 1, h);
}
}
Partition(l, h)
{
Pivot = a[l]; // Choosing the first element as pivot
i = l;
j = h;

while (i < j)
{
// Move right until an element greater than pivot is found
do
{
i++;
} while (a[i] <= pivot);

// Move left until an element smaller than pivot is found


do
{
j--;
} while (a[j] > pivot);

// Swap elements if indices haven't crossed


if (i < j)
Swap(a[i], a[j]);
}

// Swap pivot with the correct position element


Swap(a[l], a[j]);

return j; // Returning partition index


}

You might also like