 # C Programming Code Examples

## C > Sorting Searching Code Examples

### Selection Sort Program in C

``` Selection Sort Program in C In Selection sort, the smallest element is exchanged with the first element of the unsorted list of elements (the exchanged element takes the place where smallest element is initially placed). Then the second smallest element is exchanged with the second element of the unsorted list of elements and so on until all the elements are sorted. In the following C program we have implemented the same logic. Before going through the program, lets see the steps of selection sort with the help of an example: Entered elements: 23 0 -90 88 17 Step 1: -90 0 23 88 17 (23 and -90 exchanged position) Step 2: -90 0 23 88 17 (0 is at right place, no exchange needed) Step 3: -90 0 17 88 23 (23 and 17 exchanged position) Step 4: -90 0 17 23 88 (88 and 23 exchanged position) C Program - Selection sort #include<stdio.h> int main(){ /* Here x & j for loop counters, temp for swapping, count for total number of elements, number[] to store the input numbers in array. You can increase or decrease the size of number array as per requirement */ int x, j, count, temp, number; printf("How many numbers u are going to enter?: "); scanf("%d",&count); printf("Enter %d elements: ", count); // Loop to get the elements stored in array for(x=0;x<count;x++) scanf("%d",&number[x]); // Logic of selection sort algorithm for(x=0;x<count;x++){ for(j=x+1;j<count;j++){ if(number[x]>number[j]){ temp=number[x]; number[x]=number[j]; number[j]=temp; } } } printf("Sorted elements: "); for(x=0;x<count;x++) printf(" %d",number[x]); return 0; } ```

Add digits of the number single statement - In " For Loop " Condition is first tested & then body is executed. Carefully look at Semicolon at the End of " For Loop ", which tells us Two things. For loop is Bodyless. Only Condition &

Convert centimeter to meter & kilometer - C Program Code input Length in centimeter and convert it to meter and kilometer. How to convert length from centimeter to meter and kilometer in C. Input length centimeter

C Implementing Selection Sort Recursively - This C Program implements a Selection Sort. 'Selection Sort' works by finding the smallest unsorted item in the list and swapping it with the item in the current position. It is used for

C++ Program to Implement Selection Sort - This C++ program sample sort the given data using Selection Sort. Selection sort algorithm sort data by comparing one element to every other element & decide its position. The time

Function to Return a String representation - C Convert an integer to a string. Caller must allocate string array. Function returns string to allow. Use of the function in an expression. Generate digit characters in reverse order...

C++ Coding Prints Pascal Triangle Pattern - Rows of "Pascal's Triangle" are conventionally enumerated starting with row n = 0 at the top ('0th row'). Entries in each row are numbered from the left beginning with k=0 & are usually

Efficient Implementations of Disjoint Sets - numElements is the initial number of disjoint sets. Union "Two Disjoint" sets. For simplicity, we assume "root1 and root2" are distinct and represent set names. "root1" is the root of set

Find Length of Strings Without use Library - Its easier to find the length of the string using given library function, but in this program we are finding the "Length of the String" without using Library Function. In the above program