#include "stdio.h"
void main(){
int n,arr[100],i,j;
printf("Enter the size of array");
scanf("%d",&n);
printf("Enter the elements of the array");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int y;
for(int j=0;j<n;j++)
{
for(int i=j+1;i<n;i++)
{
if(arr[i]<arr[j])
{
y=arr[i];
arr[i]=arr[j];
arr[j]=y;
}
}
}
printf("Correct Order:");
for(i=0;i<n;i++)
{
printf("%d,",arr[i]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d Numbersn", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position=swap;
}
}
printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%dn", a[i]);
return 0;
}
// C program for implementation of selection sort
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
// 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;
// Swap the found minimum element with the first element
if(min_idx != i)
swap(&arr[min_idx], &arr[i]);
}
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("
");
}
// Driver program to test above functions
int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array:
");
printArray(arr, n);
return 0;
}
// Selection sort in C
#include <stdio.h>
// function to swap the the position of two elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void selectionSort(int array[], int size) {
for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {
// To sort in descending order, change > to < in this line.
// Select the minimum element in each loop.
if (array[i] < array[min_idx])
min_idx = i;
}
// put min at the correct position
swap(&array[min_idx], &array[step]);
}
}
// function to print an array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("
");
}
// driver code
int main() {
int data[] = {20, 12, 10, 15, 2};
int size = sizeof(data) / sizeof(data[0]);
selectionSort(data, size);
printf("Sorted array in Acsending Order:
");
printArray(data, size);
}
Code Example |
---|
C :: how to compareTo in java |
C :: How to pass a struct value to a pthread in c? |
C :: getchar |
C :: create array of strings in c from user input |
C :: syntax |
C :: stack push |
C :: Bitwise Operators in C language |
C :: pointer arithmetic in c |
C :: bubble sort c |
C :: mongodb read |
C :: pop and push shows black screen which needs to be pressed back flutter |
C :: KneesDev |
C :: how to merge 2 bytes into an integer |
C :: do while loop in c |
C :: boolean input in c |
C :: esp32 dhcp server |
C :: c char to int |
C :: armstrong in c |
C :: logical operators |
C :: iterating through a linked list |
C :: linux_reboot_magic2 |
C :: obstacle avoiding robot in c++ program |
C :: string text line |
C :: c to assembly online compiler |
C :: hgggggggggggggggg |
C :: VLOOKUP CHECK #N/A |
C :: python project script run |
C :: Algorithm that flips sentences and numbers |
C :: clarity ppm jasper domain commands |
C :: change no_turbo |