💻
C - Structured Programming Language
  • C - Structured Programming Language
  • Structured Programming Language
    • Chapter 0: Overview
      • Overview of Programming
      • Overview of C Programming
      • History of C Programming
      • IDE - Code::Blocks
      • New File Creation and Managing Program Files in Folders
      • C Program Basic Structure
      • Sections of the C Program
      • Steps for Using a Program
      • Flow of a Program Execution
      • First C Program - Hello World
      • Comment
      • Lexical Elements (/Tokens)
      • Keywords
      • Character Set
    • Chapter 1: Data Type, IO
      • Data Type
      • Variable
        • Variable Declaration for Primitive Data Type
        • Variable Address and Input
        • Constant and Macro
        • Variable Scope and Lifetime
        • Variable Storage Type
        • Variable Declaration Vs Definition Vs Initialization
        • Variable Default Value
        • Identifier Naming Rules and Conventions
      • Primitive Data Type: Format Specifier and Size
      • Literals
      • Primitive Data Type: Input Output
        • Input Output Function
        • Integer IO
        • Real Number IO
        • Character IO
        • String IO
        • Formatted Output
        • Formatted Input
        • Return of Scanf and Printf
      • IO Error
      • Escape Sequence / Backslash Characters
      • Data Type Modifiers
        • Data Type Size and Format Specifier
        • Input Output Using Modifiers
      • Data Type Limit
        • Integer Number
        • Floating Point Number
      • Memory Segment
      • Type Casting
      • Declaration of Derived Data Type
        • Array
        • Pointer
        • Function
        • Structure
        • Union
      • Declaration of User Defined Data Type
        • Enumeration
          • Enum Constant Value
          • Enumeration Exercise
        • Typedef
      • Declaration of Other Data Type
        • Boolean
        • Complex and Imaginary Number
        • File
        • Clock and Time
    • Chapter 2: Operator
      • Types of Operators
      • Assignment Operator
      • Arithmetic Operators
        • Math Operation
      • Increment, Decrement Operators
      • Relational Operators
      • Logical Operators
      • Bitwise Operators
      • Miscellaneous Operators
      • Operator Precedence and Associativity
      • Programming Syntax & Expression
      • Operator Exercise
    • Chapter 3: Conditional Statement
      • Truth Value
      • If Else Exercise
        • Zero, Positive, Negative - Number Sign Test
        • Voting Eligibility
        • Complex Numbers Equality
        • Different Example
          • Print a Digit into Word
          • Grade Evaluation
          • FizzBuzz
          • EVEN or ODD
          • Leap Year
          • Maximum of 2 Numbers
          • Maximum of 3 Numbers
          • Minimum of 2 Numbers
          • Minimum of 3 Numbers
      • Ternary Operator
        • Ternary Operator Exercise
      • Switch Statement
        • Switch Statement Exercise
    • Chapter 4: Loop
      • Loop Basic
      • Series Printing using Loop
      • Summation and Factorial
      • Loop Exercise
      • Number Theory
      • Break and Continue
      • Nested Loop
      • Managing Test Case
      • Goto Statement
    • Chapter 5: Pointer and Dynamic Memory Allocation
      • Pointer Fundamental Concept
        • Pointer Declaration
        • Types of Pointer
        • Pointer for Primitive Data Types
        • Pointer Referencing, Initialization, Accessing, Dereferencing
        • Pointer Size
      • Pointer Operation
        • Value Assignment/Update
        • Pointer Input Output
        • Pointer and Operator
          • Assignment Operator
          • Arithmetic Operator
          • Increment/Decrement Operator
          • Relational Operator
        • Pointer and Flow Control
      • Multilevel Pointer
      • Dynamic Memory Allocation (DMA)
        • Dynamic Memory Allocation (DMA)
        • DMA for Primitive Data Types
        • Pointer Operation after DMA
    • Chapter 6: Function & Recursion
      • Type of Function
      • Function Exercise
      • Conditional Statement and Function
      • Loop Exercise using Function
      • Pointer and Function
      • Recursion
        • Example
        • Pointer
    • Chapter 7: Array
      • Array Fundamental Concept
        • Necessity
        • Declaration (1D Array)
        • Initialization
        • Access
        • Assignment
        • Address
        • User Input
          • Test Case
      • Function and Array
      • Basic Array Operation
        • Statistical Operations
          • Summation
          • Average (Mean), Median
          • Frequency Table, Mode
          • Standard Deviation, Variance
          • Covariance, Correlation
        • Searching
          • Linear Search
          • Binary Search
          • Maximum
          • Minimum
        • Update
          • Index Based
          • Element Based
        • Delete
          • Index Based
          • Element Based
        • Insert
          • Index Based
          • Element Based
        • Sorting
          • Selection Sort
          • Insertion Sort
          • Bubble Sort
      • Other Array Operation
        • Copy
        • Reverse
        • Compare
        • Merge
        • Split
        • Cyclic Permutation
      • Pointer and Array
        • Basic
        • Insert
        • Delete
        • Summation
        • Reverse
        • Compare
        • Merge
        • Split
        • Cyclic Permutation
      • Pointer and Array
      • DMA and Array
      • 2D Array
    • Chapter 8: Character and String
      • Character
        • Declaration and Initialization
        • ASCII Code
        • Input Output
        • Conditional Statement
        • Character Arithmetic
      • String
        • String Declaration and Initialization
        • Input Output
        • Character Count in String
        • Character Conversion in String
        • String Operations
          • Substring
      • String and Pointer
    • Chapter 9: Structure
      • Basic of Structure
      • Structure Pointer
      • Function and Structure
      • Nested Structure
      • DMA and Structure
    • Chapter 10: File
    • Miscellaneous
      • Working with Math Functions
      • Working with Time Functions
      • Union
      • Preprocessor
      • Custom Header File
      • Error Handling
      • Graphics
      • Other
      • Coding Convention
      • Identifiers (Variable, Function & Structure) Naming Convention
      • Sizeof
      • Exit, Abort
      • Assert
    • Reference
      • Online Learning Material
  • Exercise
    • Problem Set 0: IDE, First C Program - 4
    • Problem Set 1: Data Types, IO - 55
      • Data Type, Variable, Format Specifier, Input, Output, String - 33
      • Data Type, Input, Output - 8
      • Data Type, Data Type Modifier, Format Specifier, Size, Limit, Address - 9
      • Formatted Input-Output - 4
    • Problem Set 2: Operator - 108
      • 2.1 Assignment Operator - 11
      • 2.2 Arithmetic Operator - 73
        • 2.2.1 Arithmetic Operator [5]
        • 2.2.2 Arithmetic Operator Exercise [10]
        • 2.2.3 Scale Conversion [25]
        • 2.2.4 Geometrical Shapes [35]
      • Bitwise Operator - 11
      • Use of Library Functions - 23
    • Problem Set 3: Conditional Statements - 47
    • Problem Set 4: Loop - 124
      • Sequence and Series - 33
      • More Loop Exercise - 52
      • Test Case - 7
      • Loop Nested - 32
    • Problem Set 5: Pointer and Dynamic Memory Allocation
      • Pointer Exercise
    • Problem Set 6: Function and Recursion - 42
      • Recursion
    • Problem Set 7: Array
      • One Dimensional Array - 114
      • Two Dimensional Array
    • Problem Set 8: Character and String - 61
      • Character - 29
      • String - 32
    • Problem Set 9: Structure
    • Problem Set 10: File
  • Solution
    • 0: IDE, First C Program
    • 1: Data Types, IO
    • 2: Operator
      • 2.1 Assignment Operator
      • 2.2 Arithmetic Operator
    • 3: Conditional Statements
      • 3.1 Conditional Statement
      • 3.2 Relational Operator
      • 3.3 Logical Operator
      • 3.4 Max, Min Median, Sorting
      • 3.5 Divisibility
    • 4: Loop
      • 4.1.1 Loop (Repetition)
      • 4.1.2 Print Sequence
      • 4.1.3 Summation of Series
      • 4.2 Loop Exercise
    • 5: Pointer and Dynamic Memory Allocation
    • 6: Function and Recursion
      • 5.1 Types of User Defined Function
      • 5.2 Exercise - Function
    • 7: Array
    • 8: Character and String
      • 7.1 Character Basic
      • 7.2 Character Input
      • 7.3 ASCII Code
      • 7.4 Character Test
      • 7.5 Character Conversion
      • 7.6 Character Arithmetic Operation
    • 9: Structure
    • 10: File
Powered by GitBook
On this page
  • Selection Sort, Ascending Order, Using Minimum
  • Selection Sort, Ascending Order, Using Maximum
  • Selection Sort, Descending Order, Using Minimum
  • Selection Sort, Descending Order, Using Maximum
  1. Structured Programming Language
  2. Chapter 7: Array
  3. Basic Array Operation
  4. Sorting

Selection Sort

Selection Sort, Ascending Order, Using Minimum

#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
void selection_asc_min(int *array, int current_size);

int main(){
    int a[TOTAL_SIZE] = {4, 6, 1, 8, 5, 2, 0, 3, 9, 7}, current_size = 10;
    
    print_array(a, current_size);
    selection_asc_min(a, current_size);
    print_array(a, current_size);

    return 0;
}

void selection_asc_min(int *array, int current_size){
    int i, j, min_index, temp;
    for(i = 0; i < current_size; i++){
        min_index = i;
        for(j = i+1; j < current_size; j++){
            if(array[j] < array[min_index]){
                min_index = j;
            }
        }
        temp = array[i];
        array[i] = array[min_index];
        array[min_index] = temp;
    }
}

void print_array(int *array, int current_size){
    int i;
    if(current_size==0){
        printf("Array is Empty\n");
    }
    else{
        printf("Array Elements are: ");
        for(i=0; i<current_size; i++){
            printf("%d ", array[i]);
        }
        printf("\n");
    }
}

Selection Sort, Ascending Order, Using Maximum

#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
void selection_asc_max(int *array, int current_size);
void selection_asc_max2(int *array, int current_size);

int main(){
    int a[TOTAL_SIZE] = {4, 6, 1, 8, 5, 2, 0, 3, 9, 7}, current_size = 10;
    
    print_array(a, current_size);
    selection_asc_max(a, current_size);
    //selection_asc_max2(a, current_size);
    print_array(a, current_size);

    return 0;
}

void selection_asc_max(int *array, int current_size){
    int i, j, max_index, temp;
    for(i = current_size-1; i > 0 ; i--){
        max_index = i;
        for(j = 0; j < i; j++){
            if(array[j] > array[max_index]){
                max_index = j;
            }
        }
        temp = array[i];
        array[i] = array[max_index];
        array[max_index] = temp;
    }
}

void selection_asc_max2(int *array, int current_size){
    int i, j, max_index, temp;
    for(i = 0; i < current_size ; i++){
        max_index = 0;
        for(j = 0; j < current_size-i; j++){
            if(array[j] > array[max_index]){
                max_index = j;
            }
        }
        temp = array[current_size-i-1];
        array[current_size-i-1] = array[max_index];
        array[max_index] = temp;
    }
}

void print_array(int *array, int current_size){
    int i;
    if(current_size==0){
        printf("Array is Empty\n");
    }
    else{
        printf("Array Elements are: ");
        for(i=0; i<current_size; i++){
            printf("%d ", array[i]);
        }
        printf("\n");
    }
}

Selection Sort, Descending Order, Using Minimum

#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
void selection_desc_min(int *array, int current_size);
void selection_desc_min2(int *array, int current_size);

int main(){
    int a[TOTAL_SIZE] = {4, 6, 1, 8, 5, 2, 0, 3, 9, 7}, current_size = 10;
    
    print_array(a, current_size);
    selection_desc_min(a, current_size);
    //selection_desc_min2(a, current_size);
    print_array(a, current_size);

    return 0;
}

void selection_desc_min(int *array, int current_size){
    int i, j, min_index, temp;
    for(i = current_size-1; i >= 0 ; i--){
        min_index = i;
        for(j = 0; j < i; j++){
            if(array[j] < array[min_index]){
                min_index = j;
            }
        }
        temp = array[i];
        array[i] = array[min_index];
        array[min_index] = temp;
    }
}

void selection_desc_min2(int *array, int current_size){
    int i, j, min_index, temp;
    for(i = 0; i < current_size ; i++){
        min_index = 0;
        for(j = 0; j < current_size-i; j++){
            if(array[j] < array[min_index]){
                min_index = j;
            }
        }
        temp = array[current_size-i-1];
        array[current_size-i-1] = array[min_index];
        array[min_index] = temp;
    }
}

void print_array(int *array, int current_size){
    int i;
    if(current_size==0){
        printf("Array is Empty\n");
    }
    else{
        printf("Array Elements are: ");
        for(i=0; i<current_size; i++){
            printf("%d ", array[i]);
        }
        printf("\n");
    }
}

Selection Sort, Descending Order, Using Maximum

#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
void selection_desc_max(int *array, int current_size);

int main(){
    int a[TOTAL_SIZE] = {4, 6, 1, 8, 5, 2, 0, 3, 9, 7}, current_size = 10;
    
    print_array(a, current_size);
    selection_desc_max(a, current_size);
    print_array(a, current_size);

    return 0;
}

void selection_desc_max(int *array, int current_size){
    int i, j, max_index, temp;
    for(i = 0; i < current_size; i++){
        max_index = i;
        for(j = i+1; j < current_size; j++){
            if(array[j] > array[max_index]){
                max_index = j;
            }
        }
        temp = array[i];
        array[i] = array[max_index];
        array[max_index] = temp;
    }
}

void print_array(int *array, int current_size){
    int i;
    if(current_size==0){
        printf("Array is Empty\n");
    }
    else{
        printf("Array Elements are: ");
        for(i=0; i<current_size; i++){
            printf("%d ", array[i]);
        }
        printf("\n");
    }
}

#include<stdio.h>

void print_array(int a[], int size);
void selection_sort_descending(int a[], int size);
void selection_sort_descending2(int a[], int size);
void selection_sort_ascending(int a[], int size);
void selection_sort_ascending2(int a[], int size);
void selection_sort_ascending_recursion(int a[], int start, int size);
void selection_sort_descending_recursion(int a[], int size);
int main(){
    int a[10] = {1, 4, 3, 5, 6, 7, 2, 8, 9, 0}, size = 10;
    int i, j, max_pos, temp;

    print_array(a, size);
    //selection_sort_ascending(a, size);
    //selection_sort_ascending2(a, size);
    //selection_sort_ascending_recursion(a, 0, size);
    selection_sort_descending_recursion2(a, size);
    //selection_sort_descending(a, size);
    //selection_sort_descending2(a, size);
    print_array(a, size);

    return 0;
}
void selection_sort_descending_recursion(int a[], int size){
    int i, j, min_pos, temp;
    if(size==0){
        return;
    }
    min_pos = 0;
    for(j=0; j<size; j++){
        if(a[j]<a[min_pos]){
            min_pos = j;
        }
    }
    temp = a[size-1];
    a[size-1] = a[min_pos];
    a[min_pos] = temp;
    selection_sort_descending_recursion(a, size-1);
}
void selection_sort_descending_recursion2(int a[], int size){
    if(size){
        int i, j, min_pos, temp;
        min_pos = 0;
        for(j=0; j<size; j++){
            if(a[j]<a[min_pos]){
                min_pos = j;
            }
        }
        temp = a[size-1];
        a[size-1] = a[min_pos];
        a[min_pos] = temp;
        selection_sort_descending_recursion(a, size-1);
    }
}
void selection_sort_ascending_recursion(int a[], int start, int size){
    int i, j, min_pos, temp;
    if(start == size){
        return;
    }
    min_pos = start;
    for(j=start+1; j<size; j++){
        if(a[j]<a[min_pos]){
            min_pos = j;
        }
    }
    temp = a[start];
    a[start] = a[min_pos];
    a[min_pos] = temp;
    selection_sort_ascending_recursion(a, start+1, size);
}

/*
{1, 4, 3, 5, 6, 7, 2, 8, 9, 0}  {0, 4, 3, 5, 6, 7, 2, 8, 9, 1}
{4, 3, 5, 6, 7, 2, 8, 9, 1}     {1, 3, 5, 6, 7, 2, 8, 9, 4}
{3, 5, 6, 7, 2, 8, 9, 4}        {2, 5, 6, 7, 3, 8, 9, 4}
{5, 6, 7, 3, 8, 9, 4}           {3, 6, 7, 5, 8, 9, 4}
{6, 7, 5, 8, 9, 4}              {4, 7, 5, 8, 9, 6}
{7, 5, 8, 9, 6}                 {5, 7, 8, 9, 6}
{7, 8, 9, 6}                    {6, 8, 9, 7}
{8, 9, 7}                       {7, 9, 8}
{9, 8}                          {8, 9}
{9}                             {9}
{}
*/

PreviousSortingNextInsertion Sort

Last updated 11 months ago