Notification
No New notification
Learn to code with PrepInsta
Check PrepInsta Coding Blogs, Core CS, DSA etc
Never Miss an OffCampus Update
Get OffCampus Updates on Social Media from PrepInsta
No New notification
Check PrepInsta Coding Blogs, Core CS, DSA etc
Get OffCampus Updates on Social Media from PrepInsta
Get Hiring Updates right in your inbox from PrepInsta
// C program for implementation of selection sort
#include
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
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 (“\n“);
}
// 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: \n“);
printArray (arr, n);
return 0;
}
#include
int main()
{
int i,j,min,temp;
int a[]={7,6,5,4,3,2,1};
int n=7;
printf(“Numbers before selection sort : “);
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
for(i=0;i<n-1;i++)
{
min=i;
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
if(min!=i)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
}
printf("Elements after sorting : ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the size of the Array”);
int n=sc.nextInt();
int[]arr=new int[n];
System.out.println(“Enter the elements of the Array”);
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
for(int i=0;i<n-1;i++){
int min=i;
for(int j=i+1;j<n;j++){
if(arr[j]<arr[min])
min=j;
}
if(min!=i){
int temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
}
for(int i=0;i<n;i++){
System.out.print(arr[i]);
}
}
}import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the Array");
int n=sc.nextInt();
int[]arr=new int[n];
System.out.println("Enter the elements of the Array");
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
for(int i=0;i<n-1;i++){
int min=i;
for(int j=i+1;j<n;j++){
if(arr[j]<arr[min])
min=j;
}
if(min!=i){
int temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
}
for(int i=0;i<n;i++){
System.out.print(arr[i]);
}
}
}
def lowestpos(mylist,start):
low=mylist[start]
lowestpos=start
for i in range(start+1,len(mylist)):
if low>mylist[i]:
low=mylist[i]
lowestpos=i
return lowestpos
def swap(mylist,x,y):
temp=0
temp=mylist[x]
mylist[x]=mylist[y]
mylist[y]=temp
mylist=list(map(int,input().split()))
for i in range(len(mylist)-1):
if mylist[i]>mylist[lowestpos(mylist,i+1)]:
swap(mylist,i,lowestpos(mylist,i+1))
print(mylist)
def sort(arr):
for i in range(len(arr)):
minpos=i
for j in range(i+1,len(arr)):
if arr[j]<arr[minpos]:
minpos=j
arr[i],arr[minpos]=arr[minpos],arr[i]
arr=list(map(int,input().split()))
sort(arr)
print(*arr)
in C++
#include
using namespace std;
int main() {
int n;
cin>>n;
int a[n];
for(int i=0;i>a[i];
for(int i=0;i<n-1;i++)
{
int idx=i;
int mini=a[i];
for(int j=i+1;ja[j])
{
mini=a[j];
idx=j;
}
}
int t=a[idx];
a[idx]=a[i];
a[i]=t;
}
for(int i=0;i<n;i++)
cout<<a[i];
return 0;
}
@DEEPANSH JOHRI
arr=list(map(int,input().split()))
for i in range (len(arr)):
minn=arr[i]
flag=False
for j in range (i,len(arr)):
if(minn>arr[j]):
minn=arr[j]
MIN_OF=arr[j]
flag=True
print(MIN_OF)
if(flag==True):
arr[arr.index(MIN_OF)],arr[i]=arr[i],arr[arr.index(MIN_OF)]
print(arr)
int main()
{
int x;
scanf(“%d”,&x);
int arr[x];
int small,temp;
for(int i=0;i<x;i++)
{
scanf("%d",&arr[i]);
}
for(int i=0;i<x-1;i++)
{
small=i;
for(int j=i+1;jarr[j])
small=j;
}
temp=arr[small];
arr[small]=arr[i];
arr[i]=temp;
}
for(int i=0;i<x;i++)
{
printf("%d ",arr[i]);
}
return 0;
}