Selection Sort Algorithm in C, Java & Python
Selection Sort Code in C
Platform:
Operating System: Windows 10
Processor: Intel® Core™ i5
IDE: Code::Blocks
Compiler: GCC (MingW / GNU GCC)
Program Code: 1 (Sorting with pre-defined inputs)
#include<stdio.h>
void selSort(int a[], int n) {
int i,j,k,min,temp;
for(i=0; i<=n-2; ++i) {
min = a[i];
k = i;
for(j=i+1; j<=n-1; ++j) {
if(a[j] < min) {
min = a[j];
k = j;
}
}
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
void display(int a[], int n) {
int i;
for(i=0; i<=n-1; ++i) {
printf("%d\t", a[i]);
}
}
ABC- TECHNOLOGY TRAINING & UPSKILLING 1
Selection Sort Algorithm in C, Java & Python
void main() {
int a[6] = {89,45,68,90,29,17};
int n = 6;
selSort(a,n);
display(a,n);
}
Program Code: 2 (Sorting with user inputs)
#include<stdio.h>
void selSort(int a[], int n) {
int i,j,k,min,temp;
for(i=0; i<=n-2; ++i) {
min = a[i];
k = i;
for(j=i+1; j<=n-1; ++j) {
if(a[j] < min) {
min = a[j];
k = j;
}
}
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
void display(int a[], int n) {
int i;
for(i=0; i<=n-1; ++i) {
printf("%d\t", a[i]);
}
}
ABC- TECHNOLOGY TRAINING & UPSKILLING 2
Selection Sort Algorithm in C, Java & Python
void main() {
int i,n;
printf("Enter the number of elements to be sorted:\n");
scanf("%d",&n);
int a[n];
printf("Enter the elements to be sorted:\n");
for(i=0; i<=n-1; ++i) {
scanf("%d",&a[i]);
}
selSort(a,n);
display(a,n);
}
Selection Sort Code in Java:
Program Code: 3 (Sorting with pre-defined inputs)
class Alpha {
public static void main(String[] args) {
int a[] = {89,45,68,90,29,17};
int n = 6;
Beta b = new Beta();
b.selSort(a,n);
Gamma g = new Gamma();
g.display(a,n);
}
}
class Beta {
void selSort(int a[], int n) {
int i,j,k,min,temp;
for(i=0; i<=n-2; ++i) {
ABC- TECHNOLOGY TRAINING & UPSKILLING 3
Selection Sort Algorithm in C, Java & Python
min = a[i];
k = i;
for(j=i+1; j<=n-1; ++j) {
if(a[j] < min)
{
min = a[j];
k = j;
}
}
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
class Gamma {
void display(int a[], int n) {
int i;
for(i=0; i<=n-1; ++i) {
System.out.print(a[i] + " ");
}
}
}
Program Code: 4 (Sorting with user inputs)
import java.util.Scanner;
class Alpha {
public static void main(String[] args) {
int i,n;
Scanner scan = new Scanner(System.in);
ABC- TECHNOLOGY TRAINING & UPSKILLING 4
Selection Sort Algorithm in C, Java & Python
System.out.println("Enter the number of elements to be
sorted:");
n = scan.nextInt();
int a[] = new int[n];
System.out.println("Enter the elements to be sorted:");
for(i=0; i<=n-1; ++i) {
a[i] = scan.nextInt();
}
Beta b = new Beta();
b.selSort(a,n);
Gamma g = new Gamma();
g.display(a,n);
}
}
class Beta {
void selSort(int a[], int n) {
int i,j,k,min,temp;
for(i=0; i<=n-2; ++i) {
min = a[i];
k = i;
for(j=i+1; j<=n-1; ++j) {
if(a[j] < min)
{
min = a[j];
k = j;
}
}
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
ABC- TECHNOLOGY TRAINING & UPSKILLING 5
Selection Sort Algorithm in C, Java & Python
class Gamma {
void display(int a[], int n) {
int i;
for(i=0; i<=n-1; ++i) {
System.out.print(a[i] + " ");
}
}
}
Selection Sort Code in Python:
Program Code: 5 (Sorting with pre-defined inputs)
def selSort(a,n):
for i in range(0,n-1):
min = a[i]
k=i
for j in range(i+1,n):
if a[j] < min:
min = a[j]
k=j
a[i],a[k] = a[k],a[i]
def display(a,n):
for i in range(0,n):
print(a[i])
a = [89,45,68,90,29,17]
n=6
selSort(a,n)
display(a,n)
ABC- TECHNOLOGY TRAINING & UPSKILLING 6
Selection Sort Algorithm in C, Java & Python
Program Code: 6 (Sorting with user inputs)
def selSort(a,n):
for i in range(0,n-1):
min = a[i]
k=i
for j in range(i+1,n):
if a[j] < min:
min = a[j]
k=j
a[i],a[k] = a[k],a[i]
def display(a,n):
for i in range(0,n):
print(a[i])
a = []
print("Enter the number of elements to be sorted:")
n = int(input())
print("Enter the elements to be sorted:")
for i in range(0,n):
elem = int(input())
a.append(elem)
selSort(a,n)
display(a,n)
YouTube Channel: https://x.co/fresher
ABC- TECHNOLOGY TRAINING & UPSKILLING 7
Selection Sort Algorithm in C, Java & Python
ABC FOR TECHNOLOGY TRAINING
Sorting Techniques are paramount no matter the path you
choose and ABC is here to help you through it all.
Master disruptive in-demand technologies at ABC to kick-start
your IT career.
Benefits of our courses:
• Classroom Training with World-Class Infrastructure
• Concept Visualization through Animations and Hands-on
Coding
• Acquire 25+ in-demand Skills & Tools
• Design & Develop 6 Capstone Projects
• Industry Oriented Training
• Online tests & Mock interviews
• Globally Accepted Certification
• Mentorship Programmes
• Dedicated Student Success Manager
• Easy EMI facilities
• Refund Scheme
• 100% Job Guaranteed
• A handful of opportunities to fulfil your ambitions
ABC- TECHNOLOGY TRAINING & UPSKILLING 8
Selection Sort Algorithm in C, Java & Python
A GLIMPSE OF OUR PREMIUM LABS
WITH 600+ HIGH-SPEED INTERNET ENABLED SYSTEMS
For more updates & information on our Unified Courses, stalk us at:
Instagram: https://goo.gl/4BR1HA
Facebook: https://goo.gl/KjG9Hc
LinkedIn: https://goo.gl/pKRPEM
Twitter: https://goo.gl/vXyUb2
Contact us: 7676500600
ABC- TECHNOLOGY TRAINING & UPSKILLING 9