sorting – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 10 Mar 2024 17:00:27 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg sorting – Programmerbay https://programmerbay.com 32 32 C Program to Sort an array using Selection Sort in Ascending Order https://programmerbay.com/program-to-sort-an-array-using-selection-sort/ https://programmerbay.com/program-to-sort-an-array-using-selection-sort/#respond Sun, 03 Mar 2024 17:20:38 +0000 https://www.programmerbay.com/?p=2879 The program sorts uses Selection Sort technique to sort the given array. There are bunch of sorting mechanisms such as Bubble sort, Insertion sort and more to achieve this functionality. In this, we’ll be only focusing Selection Sort .

Selection Sort

Selection sort is a sorting technique wherein at each iteration, a minimum element is selected from an unsorted array and compared with all other remaining elements. if an element is found that is smaller than already assumed minimum element,  swapping would be performed.

It divides an array into two parts. One that is already sorted and another one which is still unsorted that need to be arranged.

From unsorted sublist, the algorithm finds the minimum element and adds on to sorted category. It ends when we get a single consolidated sorted sublist.

Pseudocode

FOR i=0 to Array.Length
minimum = i
FOR j=0 to Array.Length
if A[min]< A[j])
Swap A[min] with A[j]
End IF
END FOR
END FOR

C program to sort an array using Selection Sort in Ascending Order

#include<conio.h>
#include<stdio.h>
#define size 5
void main()
{
int arr[size],i,j,temp,min;
printf("Please enter elements: \n "); 
for(i=0;i<=size-1;i++)
{
scanf("%d",&arr[i]);
}

for(i=0;i<=size-1;i++)
{
min= i;
for(j=0;j<=size-1;j++)
{
if(arr[min]<arr[j]){
temp = arr[j];
arr[j] =arr[min];
arr[min] = temp;
}
}
}

printf(" \n Entered Elements :");

for(i=0;i<=size-1;i++)
{
printf("\n %d ",arr[i]);
} 
getch();

}

Output:

Screenshot from 2024 03 10 22 22 03

 

How does Selection Sort work?

Screenshot from 2024 03 10 22 28 33

 

Lastly, it is an inefficient algorithm for the large dataset as it requires O(n2) comparisons in all cases worst, average and best case. It is simple and occasionally has an upper hand over other complicated algorithms.

]]>
https://programmerbay.com/program-to-sort-an-array-using-selection-sort/feed/ 0
C Program to sort an array using Merge Sort with its Time Complexity https://programmerbay.com/program-to-sort-an-array-using-merge-sort-with-its-time-complexity/ https://programmerbay.com/program-to-sort-an-array-using-merge-sort-with-its-time-complexity/#respond Fri, 12 Jun 2020 16:22:39 +0000 https://www.programmerbay.com/?p=2904 The program sort an array using Merge Sort technique.

Merge Sort

Merge sort is one of the most common sorting techniques and it is based on the divide and conquer paradigm.

This paradigm focuses on the recurring nature of the problem and thus divides it into various branches (or subproblems). This division for sub-problems depends upon the complexity of the problem at hand.

Now, these sub-problems are further broken down recursively till they are solved at the very end and then, are retracted in a backward fashion and all these results are merged together to give a final solution of the problem.

C program to Sort an array using Merge Sort

#include<stdio.h>

void merging(int arr[],int  a,int b,int a1,int b1)

{

int temp[50];

int i,j,k;

i=a;

j=a1;

k=0;

while(i<=b && j<=b1)

{

if(arr[i]<arr[j])

temp[k++]=arr[i++];

else

temp[k++]=arr[j++];

}

while(i<=b)

temp[k++]=arr[i++];

while(j<=b1)

temp[k++]=arr[j++];

for(i=a,j=0;i<=b1;i++,j++)

arr[i]=temp[j];

}

void merge_sort(int arr[],int i,int j)

{

int mid;

if(i<j)

{

mid=(i+j)/2;

merge_sort(arr,i,mid);

merge_sort(arr,mid+1,j);

merging(arr,i,mid,mid+1,j);

}

}

int main()

{

int arr[30],n,i;

printf("Enter no of elements:");

scanf("%d",&n);

printf("Enter array elements:");

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

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

merge_sort(arr,0,n-1);

printf("\nSorted array is :");

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

printf("%d ",arr[i]);

return 0;

}

 

Output:

merge sort

 

The time complexity of the merge sort is O (n log n), because of the following reasons:

  • The divide phase, finds out the midpoint of each subsequent sub-array, which results in O(1) time for each step.
  • In the conquer phase, this is n/2 because this step recursively sorts 2 sub-arrays.
  • The merge phase takes O(n) time because it merges all the elements of the array.

]]>
https://programmerbay.com/program-to-sort-an-array-using-merge-sort-with-its-time-complexity/feed/ 0
C Program to sort an array using Quicksort in Ascending Order https://programmerbay.com/program-to-sort-an-array-using-quicksort/ https://programmerbay.com/program-to-sort-an-array-using-quicksort/#respond Thu, 11 Jun 2020 12:05:22 +0000 https://www.programmerbay.com/?p=2894 Quicksort is based on Divide and Conquer paradigm. It works by partitioning the given input array into two subarrays and sorts them through recursive calls until they get sorted.

In this, a pivot element is used that acts as a base for partitioning the array, where element lesser than pivot element arranged at the left side and bigger ones at the right side.

It requires O(n log n) running time to sort the given array when it comes to the average case.

Therefore, it can be used to sort a large set of elements. However, in the worst case, the running time of this algorithm hops up to O(n2).

Pseudocode

Quicksort( Array,first,last)
IF first <last
partitionpoint = Partition(arr,first,last);
Quicksort(Array,first,partitionpoint-1)
Quicksort(Array,partitionpoint+1,last)

Partitioning

Partition(Array,first,last)
pivot = arr[last]
i=first-1
FOR j=first to last - 1
IF Array[j]<pivot
i=i+1
exchange Array[i] with Array[j]
END IF
END FOR
exchange Array[i] with Array[last]
return i+1

C program to sort an array using Quicksort in Ascending Order

#include<stdio.h>
#define size 5

void quicksort(int arr[],int first,int last)
{

if(first <last)
{
int partitionpoint;
partitionpoint = sort_me(arr,first,last);
quicksort(arr,first,partitionpoint-1);
quicksort(arr,partitionpoint+1,last);
}

}

int sort_me(int arr[],int first,int last)
{
int pivot = arr[last],j,temp,n;
int i=first-1;
for(j=first;j<last;j++)
{
if(arr[j]<pivot)
{
i++;
temp =arr[j];
arr[j]=arr[i];
arr[i]=temp;

}
}
temp =arr[i+1];
arr[i+1]=arr[last];
arr[last]=temp;

return i+1;
}

int main()
{
int arr[size],j,temp,n;
printf("Please enter elements: \n "); 
for(n=0;n<=size-1;n++)
{
scanf("%d",&arr[n]);
}

quicksort(arr,0,size-1);

printf(" \n Sorted Elements :");

for(n=0;n<=size-1;n++)
{
printf("\n %d ",arr[n]);
} 
return 0;

}

 

Output:

quicksort

]]>
https://programmerbay.com/program-to-sort-an-array-using-quicksort/feed/ 0
C program to sort an array using Bubble Sort in Ascending Order https://programmerbay.com/program-to-sort-an-array-using-bubble-sort/ https://programmerbay.com/program-to-sort-an-array-using-bubble-sort/#respond Wed, 10 Jun 2020 10:56:44 +0000 https://www.programmerbay.com/?p=2867 The program sorts an array in ascending order based on the given input. There are various sorting algorithms to achieve this functionality Such as Selection Sort, Insertion sort and more . In this, We’ll be using Bubble Sort algorithm.

Bubble sort is one of the simplest and most popular algorithms, but inefficient. It repeatedly compares each element with their adjacent elements and performs swapping if required.

Pseudocode

FOR i=0 to Array.Length
FOR j=Array.Length-1 to i
IF A[j]< A[j-1])
Swap A[j] and A[j-1]
END IF
END FOR
END FOR

C program to sort an array using Bubble Sort in Ascending Order

#include<conio.h>
#include<stdio.h>
#define size 5
void main()
{
int arr[size],i,j,temp;
printf("Please enter elements: \n "); 
for(i=0;i<=size-1;i++)
{
scanf("%d",&arr[i]);
}

for(i=0;i<=size-1;i++)
{
for(j=size-1;j>i;j--)
{
if(arr[j]<arr[j-1]){
temp = arr[j];
arr[j] =arr[j-1];
arr[j-1] = temp;
}

}

}

printf(" \n Entered Elements :");

for(i=0;i<=size-1;i++)
{
printf("\n %d ",arr[i]);
} 
getch();

}

 

Output:

bubble sort 1

How does Bubble Sort program work?

bubble sort working

It requires a constant amount O(1) of additional memory space and O(n2) comparisons in the worst case and average case to sort an array.

However, this sorting technique is too slow and impractical if we compare it with Insertion sort

]]>
https://programmerbay.com/program-to-sort-an-array-using-bubble-sort/feed/ 0
C Program to sort an array using Insertion sort in Ascending order https://programmerbay.com/program-to-sort-an-array-using-insertion-sort/ https://programmerbay.com/program-to-sort-an-array-using-insertion-sort/#respond Tue, 09 Jun 2020 18:33:09 +0000 https://www.programmerbay.com/?p=2846 The program sorts an array based on given input. There are bunch of sorting techniques such as Bubble sort, Selection sort and more to achieve this functionality. In this, we’ll be discussion Insertion Sort technique.

Insertion sort

In this sorting technique, the number that needs to be sorted is known as Key. This algorithm is efficient for sorting a small unit of elements.

It can be considered as a way that people use to sort playing cards. A set of cards facing towards the table are picked one by one and put at their correct place.

we compare a new card lifted from the table with the existing ones in our hand.

playing cards

Similarly, in insertion sort at each iteration, an element from the input dataset is taken out and placed to the location where it belongs to be. It is followed until all the inputs get placed and sorted.

Pseudocode

for j=1 to Array.Length

key=arr[j]
k=j-1;
while k>=0 && arr[k]>key
arr[k+1] = arr[k]
arr[k]=key
k= k-1
End While
End for

C program to sort an array using Insertion Sort in Ascending Order

#include<conio.h>
#include<stdio.h>
#define size 6
void main()
{
int arr[size],i,j,k,key,temp;
printf("Please enter elements: \n "); 
for(i=0;i<=size-1;i++)
{
scanf("%d",&arr[i]);
}

for(j=1;j<=size-1;j++)
{
key=arr[j];
k=j-1;
while (k>=0 && arr[k]>key)
{
arr[k+1] = arr[k];
arr[k]=key;
k=k-1;
}
}

printf(" \n Entered Elements :");

for(i=0;i<=size-1;i++)
{
printf("\n %d ",arr[i]);
} 
getch();
}

 

Output:

insertion sort

How does it work?

insertion sort working

It requires a constant amount O(1) of additional memory space and O(n2) comparisons in the worst case and average case to sort an array.

However, sorting time to sort the same size of elements can be varied depending on how much a given array is already sorted.

]]>
https://programmerbay.com/program-to-sort-an-array-using-insertion-sort/feed/ 0