The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted […]

The post Data Structure – Selection sort algorithm appeared first on Panayiotis Georgiou.

]]>The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right. (The example code is written in C programming language)

`#include`

void selection_sort(int a[],int n);

{

int i,j,pos,small,temp;

for(i=0;i<n-1;i++)

{

small=a[i]; /* Initial small number in ith pass */

pos=i; /* Position of smaller number */

/* Find the minimum of remaining elements along with the position */

for(j=i+1;j<n;j++)

{

if(a[i]<small)

{

small=a[j];

pos=j;

}

}

/* Exchange ith item with least item */

temp=a[pos];

a[pos] = a[i];

a[i]=temp;

}

}

void main()

{

int i,n,a[20];

printf("Enter the number of elements to sort");

scanf("%d",&n);

printf("Enter %d elements to sort ",n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

selection_sort(a,n)

printf("The sort elements are");

for(i=0;i<n;i++)

printf("%d",&a[i]);

}

The post Data Structure – Selection sort algorithm appeared first on Panayiotis Georgiou.

]]>Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. (The example code is written in C programming language) In bubble sort we’ll compare each element of list to […]

The post Data Structure – Bubble sort algorithm appeared first on Panayiotis Georgiou.

]]>**Bubble sort**, sometimes incorrectly referred to as **sinking sort**, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.

(The example code is written in C programming language)

- In bubble sort we’ll compare each element of list to the element that follows it. If the current element is greater than the element at next location, then they are in the wrong order, and we’ll swap them. In this way, the element with large value will be percolate upward. Now the last element in the array is guaranteed to be where it belongs. In the next step we do exactly the same thing. Repeating the above step will result a sorted array.
- The bubble sort can be made to work in the opposite direction. moving the least value to the beginning of the array on each pass. This is sometimes referred to as a stone sort.
- In this sorting algorithm, multiple Swapping take place in one pass. Smaller elements move or ‘bubble’ up to the top of the list, hence the name given to the algorithm.
- In this method adjacent members of the list to be sorted are compared. If the item on top is greater than the item immediately below it, they are swapped. This process is carried on till the list is sorted.
**Note:**At least one pass is required to check whether the items are sorted. So, the best case time complexity is O(1).

`#include`

void bubble_sort(int a[], int n)

{

int i; /* To access subsequent item while comparing*/

int j; /* Keep track of the passes */

int temp; /* Used to exchange the item */

int sum; /* Holds the total number of exchanges */

int pass; /*Holds the number of passes required */

int exchag; /* Holds the number of exchanges in each pass */

int flag; /* Indicate any exchange has been done or not */

sum = 0;

pass = 0;

for(j=1;j<n;j++)

{

exchg = 0; /* number of exchanges just before the pass */

flage = 0; /* No exchange been done */

for(i=0;i<n-j;i++) { if(a[i]>=a[i+1])

{

/* Exchange and update the number of exchange in the current pass*/

temp=a[i];

a[i]=a[i+1];

a[i+1=temp;

exchg++;

sum++ /* Update the total number of exchanges */

flag=1; /* Exchange has been done */

}

}

pass++; /* update the number of passes */

printf("Number of exchanges in pass : %d=%dn",j,exchg);

print("Total number of exchanges = %dn",sum);

}

void main()

{

int i,n,a[20];

printf("Enter the number of items to sort");

scanf("%d",&n);

print("Enter the items to sort");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

bubble_sort(a,n);

printf("The sorted items are");

for(i=0;i<n;i++)

{

printf("%dn",a[i]);

}

}

The post Data Structure – Bubble sort algorithm appeared first on Panayiotis Georgiou.

]]>Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. (The example code is written in C programming language) An insertion sort visits each element of the array, in turn. As it visits a particular element, it scans the array from the beginning to end […]

The post Data Structure – Insertion sort algorithm appeared first on Panayiotis Georgiou.

]]>*(The example code is written in C programming language)*

- An insertion sort visits each element of the array, in turn. As it visits a particular element, it scans the array from the beginning to end to determines where in that segment of the array the current value belongs.
- It then inserts the current value in that location and shifts every element of the array to the right, up to the present location. It then goes on to the next location (value) in the array.
- Notice that one index is going from 0 to n and for each such value and another index is scanning the array from 0 to the value of the first index. The result of this is – that this type of sort is O(n2). Insertion sort is slower.

`#include`

void insertion_sort(int a[],int n)

{

int i,j,item;

for(i=0;i<n;i++) { /* item to be inserted */ item = a[i]; /* Try from (i-1)th position */ j=i-1; while(j>=0 && item<a[j])

{

A[j+1] = a[j] /* Move the item to the next position */

j--; /* and update the position */

}

A[j+1]=item; /* appropriate position found and so insert item */

}

}

void main()

{

int i, n,a[20];

printf("Enter the no. of elements to sort n");

scanf("%d", &n);

printf("Enter n elements n");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

insertion_sort(a,n);

printf("The sorted array is n");

for(i=0;i<n;i++)

printf("%dn",a[i]);

}

The post Data Structure – Insertion sort algorithm appeared first on Panayiotis Georgiou.

]]>