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 :: function for quicksort in c 
C :: c style array 
C :: Futter Square Button 
C :: c check if character is a digit 
C :: c binary search 
C :: mutex c 
C :: check if string in string c 
C :: char array to int c 
C :: go optional parameters 
C :: Syntax To Take Input In C 
C :: slug urls django 
C :: append to list in c 
C :: what is string::npos 
C :: how to print in c 
C :: hello word in c 
C :: c programming language 
C :: why there is return 0 used in c 
C :: c find last element in array 
C :: c bits 
C :: how to change background color in c programming 
C :: Leap year using function in c 
C :: imprimir matriz 
C :: adding a node in the front on a linked list 
C :: stddef.h 
C :: get boolean from localstorage 
C :: how to print logs when doing unit-testing in rust 
C :: how to input a string into a char array cpp 
C :: c for result 
C :: ouverture du fichier c 
C :: main prototype 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =