• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc
Tech Stack Journal

Tech Stack Journal

Selection Sort

July 26, 2020 by Admin Leave a Comment

Contents

  • 1 Overview
  • 2 Complexity
  • 3 Selection Sort in Java
  • 4 Selection Sort in C language
  • 5 Selection Sort in C++

Overview

Selection sort is the most basic way of sorting an Array. We compare each element with its subsequent elements and swap them if one is smaller than the other.

Complexity

The complexity of selection sort algorithm for worst case, average cases and best cases is O(n2).

Selection Sort in Java

package com.techstackjournal;

public class SelectionSort {

	public static void main(String[] args) {

		int[] nums = { 10, 4, 9, 3, 1, 5, 8, 2, 7 };
		int temp;

		for (int i = 0; i < nums.length - 1; i++) {
			for (int j = i + 1; j < nums.length; j++) {
				if (nums[i] > nums[j]) {
					temp = nums[i];
					nums[i] = nums[j];
					nums[j] = temp;
				}
			}
		}

		for (int i : nums) {
			System.out.print(i + " ");
		}

	}

}

Selection Sort in C language

#include <stdio.h>

void sort(int *nums, int len) {
	int temp;

	for (int i = 0; i < 9; i++) {
		for (int j = i + 1; j < 10; j++) {
			if (nums[i] > nums[j]) {
				temp = nums[i];
				nums[i] = nums[j];
				nums[j] = temp;
			}
		}
	}
}

int main() {

	int nums[] = { 10, 4, 9, 3, 1, 5, 8, 2, 7, 6 };

	printf("Unsorted array:\n");
	for (int i = 0; i < 10; i++) {
		printf("%d ", nums[i]);
	}

	printf("\n\nSorted array:\n");
	sort(nums, 10);

	for (int i = 0; i < 10; i++) {
		printf("%d ", nums[i]);
	}

	return 0;
}

Selection Sort in C++

#include <iostream>

using namespace std;

void sort(int *nums, int len)
{
    int temp;

    for (int i = 0; i < 9; i++)
    {
        for (int j = i + 1; j < 10; j++)
        {
            if (nums[i] > nums[j])
            {
                temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
        }
    }
}

int main()
{

    int nums[] = { 10, 4, 9, 3, 1, 5, 8, 2, 7, 6 };

    cout<<"Unsorted array:\n";
    for (int i = 0; i < 10; i++)
    {
        cout<<nums[i]<<" ";
    }

    cout<<"\n\nSorted array:\n";
    sort(nums, 10);

    for (int i = 0; i < 10; i++)
    {
        cout<<nums[i]<<" ";
    }

    return 0;
}

Filed Under: Algorithms

Previous Post: « Binary Search
Next Post: Spring XML Configuration and Constructor Injection »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


Primary Sidebar

More to See

Arrays.asList in Java Examples

February 21, 2021 By Admin

[Solved] Why List.add throws UnsupportedOperationException in Java?

February 20, 2021 By Admin

Secondary Sidebar

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Misc
  • Node.js
  • Spring Core
  • Windows

Archives

  • February 2021 (6)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc

Recent

  • How to Make File Explorer Open to This PC instead of Quick Access in Windows 10
  • Arrays.asList in Java Examples
  • [Solved] Why List.add throws UnsupportedOperationException in Java?
  • How to Convert an Array to List in Java?
  • How Many Spaces in a Tab?

Search

Copyright © 2021 · Tech Stack Journal · Log in