Search
 
SCRIPT & CODE EXAMPLE
 

C

Selection Sort in C

#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]);
    }
}
Comment

selection sort algorithm in c

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;
}
Comment

selection sort c

// 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;
}
Comment

selection sort in c

// 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);
}
Comment

PREVIOUS NEXT
Code Example
C :: lichess puzzles 
C :: how compress string in c 
C :: converting strings to numbers in c 
C :: array of strings in c 
C :: actionbar content color in android 
C :: c change value of const 
C :: c program for swapping of two numbers 
C :: pid of a process in c 
C :: fseek function in c 
C :: iterate through enum in qt 
C :: choose random number with weight 
C :: sleep in c 
C :: input value from terminal to c 
C :: command args c 
C :: Compile multiple C files 
C :: Multi-line Comments in C 
C :: size of int in c 
C :: how to input a string into a char array cpp 
C :: hostbuilder add environment variables 
C :: printing a string with putchar 
C :: arduino vscode upload choosing sketch 
C :: get string from ptrace registery 
C :: scranton inhabitants 
C :: libreoffice reference cell in different sheet with sheet name with space 
C :: BSTNode root 
C :: visa germany algeria 
C :: Letters and Digits Total 
C :: C Nested if...else 
C :: c refresher 
C :: how to compress image in c 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =