0% found this document useful (0 votes)
0 views

Algorithm

The document provides pseudocode for four sorting algorithms: Bubble Sort, Selection Sort, Insertion Sort, and Quick Sort. Each algorithm is described with its respective logic for sorting an array of numbers. The document outlines the steps involved in each sorting method, highlighting their unique approaches to organizing data.

Uploaded by

ruhulaminrafi206
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Algorithm

The document provides pseudocode for four sorting algorithms: Bubble Sort, Selection Sort, Insertion Sort, and Quick Sort. Each algorithm is described with its respective logic for sorting an array of numbers. The document outlines the steps involved in each sorting method, highlighting their unique approaches to organizing data.

Uploaded by

ruhulaminrafi206
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Bubble Sort

for(i=N-1; i>=0; i--){


for(j=0; j<i; j++){
/* compares adjacent numbers */
if(array[j] > array[j+1]){
/* swaps the number if condition satisfies */
tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
}
}
}

Selection Sort

for(i=0; i<N-1; i++){


minIndex = i;
for(j=i+1; j<N; j++){
/* compares adjacent numbers */
if(array[j] < array[minIndex]){
minIndex = j;
}
}
/* swaps the number if condition satisfies */
tmp = array[minIndex];
array[minIndex] = array[i];
array[i] = tmp;
}

Insertion Sort
for (i=1; i<N; i++) {
tmp = array[i];
j=i;
while(j>0 && tmp<array[j-1]){
array[j] = array[j-1];
j--;
}
array[j]=tmp;
}

Quick Sort

function quicksort('array'){
if length('array') ≤ 1
return 'array' // an array of zero or one elements is already sorted

select and remove a pivot element 'pivot' from 'array' // see 'Choice of pivot' below

create empty lists 'less' and 'greater'

for ('x' in 'array'){


if ('x' ≤ 'pivot')
append 'x' to 'less'
else
append 'x' to 'greater'
}
return concatenate(quicksort('less'), list('pivot'), quicksort('greater')) // two recursive calls
}

You might also like