// C program for insertion sort
#include <math.h>
#include <stdio.h>
void insertionSort(int arr[], int n);
// A utility function to print a sorted array of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver code
int main()
{
int arr[15],n,i;
printf("enter number of elements in array \n");
scanf("%d", &n);
printf("enter array values to sort\n");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
#include <stdio.h>
void insertionSort(int arr[], int n);
// A utility function to print a sorted array of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver code
int main()
{
int arr[15],n,i;
printf("enter number of elements in array \n");
scanf("%d", &n);
printf("enter array values to sort\n");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
printf("\n sorted array using insertion sort \n");
}
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
printf("\n sorted array using insertion sort \n");
}
OUTPUT:
enter number of elements in array
8
enter array values to sort
43
23
56
76
35
59
31
42
sorted array using insertion sort
23 31 35 42 43 56 59 76
// C program for bubble sort
#include <math.h>
#include <stdio.h>
void bubbleSort(int arr[], int n);
// A utility function to print a sorted array of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver code
int main()
{
int arr[15],n,i;
printf("enter number of elements in array \n");
scanf("%d", &n);
printf("enter array values to sort\n");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
bubbleSort(arr, n);
printArray(arr, n);
return 0;
}
/* Function to sort an array using bubble sort*/
void bubbleSort(int arr[], int n)
{
int i, j, temp;
for (i = 0; i < n - 1; i++)
// Last i elements are already in place
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
printf("sorted array by using bubble sort \n");
}
OUTPUT:
enter number of elements in array
8
enter array values to sort
14
37
2
39
57
48
66
69
sorted array by using bubble sort
2 14 37 39 48 57 66 69
// C program for selection sort
#include <math.h>
#include <stdio.h>
void selectionSort(int arr[], int n);
// A utility function to print a sorted array of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver code
int main()
{
int arr[15],n,i;
printf("enter number of elements in array \n");
scanf("%d", &n);
printf("enter array values to sort\n");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
selectionSort(arr, n);
printArray(arr, n);
return 0;
}
/* Function to sort an array using selection sort*/
void selectionSort(int arr[], int n)
{
int i, j, min_idx, temp;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
printf("sorted array by using selection sort \n");
}
OUTPUT:
enter number of elements in array
8
enter array values to sort
34
25
16
39
48
57
69
40
sorted array by using selection sort
16 25 34 39 40 48 57 69
Comments
Post a Comment