Problem 2

33 comments on “Problem 2”


  • Raman

    #include
    int main() {
    int a[9]={13,11,17,5,9,15,21,31,15};
    int i,j,temp;

    for(i=0;i<9;i++){
    int minpos=i;
    for(j=i;ja[j]){
    minpos=j;
    }
    }
    temp=a[minpos];
    a[minpos]=a[i];
    a[i]=temp;
    }
    for(i=0;i<9;i++){
    printf("%d ",a[i]);
    }
    return 0;
    }


  • Manni

    public class SelSorting {
    public static void main(String[] args) {
    // Scanner sc=new Scanner(System.in);
    int arr[] = { 14, 33, 27, 10, 35, 19, 42, 44 };
    selSort(arr);
    printArr(arr);
    }

    public static int[] selSort(int[] arr) {
    for (int i = 0; i < arr.length; i++) {
    int index = i;
    for (int j = i + 1; j < arr.length; j++) {
    if (arr[j] < arr[index]) {
    index = j;
    }
    }
    int c = arr[i];
    arr[i] = arr[index];
    arr[index] = c;
    }
    return arr;
    }

    public static void printArr(int[] arr) {
    System.out.println();
    for (int i = 0; i < arr.length; i++) {
    System.out.print(arr[i] + " ");
    }
    }
    }


  • kartik

    int index=0;
    main(){
    int arr[]={14,33,27,10,35,19,42,44};
    int n=8;
    for(int i=0;i<=n-2;i++){
    int minimum=arr[i];
    for(int j=i+1;j<n;j++){
    if(arr[j]<minimum){
    minimum=arr[j];
    index=j;
    }
    }
    if(minimum!=arr[i]){
    int temp=arr[i];
    arr[i]=minimum;
    arr[index]=temp;
    }
    cout<<arr[i]<<" ";
    }
    }


  • swapnil

    size=int(input())
    arr=[int(i) for i in input().split(‘ ‘)]
    for i in range(len(arr)):
    flag=1
    while flag==1:
    for j in range(i,len(arr)):
    if arr[i]>arr[j]:
    t=arr[i]
    arr[i]=arr[j]
    arr[j]=t
    flag=1
    break
    flag=0
    print(arr)


    • amit

      #include

      int main()
      {
      int ch[]={14,33,27,10,35,19,42,44};
      int size;
      int temp;
      int i;
      int count=1;
      size = sizeof(ch)/sizeof(int);
      //printf(“%d”,size);
      for(int j=0;j<size;j++)
      {
      i=count;
      while(ich[i])
      {
      temp=ch[j];
      ch[j]=ch[i];
      ch[i]=temp;
      }
      i++;

      }
      count++;
      printf(“%d “,ch[j]);
      }

      return 0;
      }


  • Nikkala

    import java.util.*;
    class anil
    {
    public static void main(String args[])
    {
    Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    int a[]=new int[n];
    int temp;
    System.out.println(“enter the elements”);
    for(int i=0;i<n;i++)
    {
    a[i]=s.nextInt();
    }
    for(int i=0;i<n;i++)
    {
    for(int j=i+1;ja[j])
    {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
    }
    }
    }
    System.out.println(“sorting of the elements”);
    for(int i=0;i<n;i++)
    {
    System.out.print(a[i]);
    }

    }
    }


  • Nikkala

    import java.util.*;
    public class anil
    {
    public static void main(String args[])
    {
    Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    int a[]=new int[n];
    int temp;
    System.out.println(“enter the elements”);
    for(int i=0;i<n;i++)
    {
    a[i]=s.nextInt();
    }
    for(int i=0;i<n;i++)
    {
    for(int j=0;ja[j])
    {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
    }
    }
    }
    System.out.println(“sorting of the elements”);
    for(int i=0;i<n;i++)
    {
    System.out.println(a[i]);
    }

    }
    }


  • goutham

    #include
    using namespace std;
    int main()
    {int a[9]={13,11,17,5,9,15,21,31,15};
    int i,j,b;
    for(i=0;i<=7;i++)
    {for(j=i+1;j<=8;j++)
    {if(a[j]<a[i])
    {b=a[i];a[i]=a[j];a[j]=b;}
    }
    }
    for(i=0;i<9;i++)
    {cout<<a[i]<<" ";}
    return 0;}


  • subhojit

    #Simple Python Code

    arr=[int(x) for x in input().split(” “)]
    ct=1
    j=0
    for i in range(ct,len(arr),1):
    z=arr[slice(ct,len(arr))]
    index=arr.index(min(z))
    if arr[j]>min(z):
    arr[j],arr[index]=arr[index],arr[j]
    ct+=1
    j+=1
    print(arr)


  • Prakhar

    #selection sort
    #select min and add in unsorted part
    l=list(map(int,input().split()))
    for i in range(len(l)):
    m=i
    for j in range(i+1,len(l)):
    if(l[m]>l[j]):
    m=j
    l[i],l[m]=l[m],l[i]
    print(l)


  • Srimukhi

    #include
    int main()
    {
    int n,i,j,a[100],min=0,temp=0;
    printf(“Enter the size of a array : “);
    scanf(“%d”,&n);
    printf(“Enter the elements into the array : \n”);
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    }
    for(i=0;i<n-1;i++)
    {
    min=a[i];
    for(j=i+1;j<n;j++)
    {
    if(min<a[j])
    {
    min=a[j];
    }
    }
    temp=a[i];
    a[i]=min;
    min=temp;
    }
    for(i=0;i<n;i++)
    {
    printf("%d",a[i]);
    }
    }
    i tried like this but i am not getting the output can u pls help me why


  • Mohd Saif

    arr=[14,33,27,10,35,19,42,44]
    for i in range(0,len(arr)):
    minpos=i
    for j in range(i+1,len(arr)):
    if(arr[j]<arr[minpos]):
    minpos=j
    temp=arr[i]
    arr[i]=arr[minpos]
    arr[minpos]=temp
    print(arr)


  • naveen k sharma

    def selection_sort(arr1):

    for i in range(0,len(arr1)):
    min_value = i
    for j in range(i+1,len(arr1)):
    if(arr1[min_value]>arr1[j]):
    min_value = j
    temp=arr1[min_value]
    arr1[min_value] = arr1[i]
    arr1[i] = temp
    for j in range(0,len(arr1)):
    print(arr1[j],end=” “)

    if __name__==’__main__’:
    n = int(input())
    arr1 = [None]*n
    for i in range(0,n):
    arr1[i] = input()
    selection_sort(arr1)