Problem 2

33 comments on “Problem 2”


  • pjanhabi584

    #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]);
    }
    }


  • Swarnendu

    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]);
    }

    }

    }


  • sagarbiswasbunny

    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)


  • Ujjawal

    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)


  • dee.pansh18johri

    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


  • Gourav

    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)


  • Rahul

    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;
    }