Problem 2

33 comments on “Problem 2”


  • Rahul

    int main()
    {
    int n;
    scanf(“%d”,&n);
    int arr[n];
    int small,temp;
    for(int i=0;i<n;i++)
    {
    scanf("%d",&arr[i]);
    }
    for(int i=0;i<n-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<n;i++)
    {
    printf("%d ",arr[i]);
    }
    return 0;
    }


  • Shivam

    #include
    using namespace std;
    int main()
    {
    int a[5];
    int i;
    for(i=0;i>a[i];
    }
    for(i=0;i<=4;i++)
    {
    for(int j=i+1;ja[j])
    {
    int temp = a[i];
    a[i]=a[j];
    a[j]=temp;
    }
    }
    }
    for(i=0;i<5;i++)
    {
    cout<<a[i]<<" ";
    }
    }


  • GAURAV

    #include

    using namespace std;
    void swap(int *a,int *b){
    int temp=*a;
    *a=*b;
    *b=temp;
    }

    int main()
    { int save=0;
    int min;
    int arr[] = { 64, 25, 12, 22, 11 };
    int n=sizeof(arr)/sizeof(arr[0]);
    for(int i=0;i<n-1;i++){
    min=arr[i];
    for(int j=i+1;j<n;j++){
    if(arr[j]<min){
    min=arr[j];
    save=j;
    }
    }
    swap(&arr[i],&arr[save]);
    }
    for(int i=0;i<n;i++)
    cout<<arr[i]<<" ";

    return 0;
    }


  • Kush

    import java.util.*;
    class SelectionSort
    {
    public static void main(String args[])
    {
    Scanner sc =new Scanner(System.in);
    int n = sc.nextInt();
    int arr[] = new int[n];

    for(int i=0;i<n;i++)
    {
    arr[i] =sc.nextInt();
    }

    for(int i=0;i<n;i++)
    {
    int temp =0;
    for(int j=i+1;jarr[j])
    {
    temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    }
    else
    continue;

    }
    }
    System.out.println(“After Sorting”);

    for(int i=0;i<n;i++)
    {
    System.out.println(arr[i]);
    }
    }
    }


  • geetanjali kumari

    /******************************************************************************

    Welcome to GDB Online.
    GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
    C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
    Code, Compile, Run and Debug online from anywhere in world.

    *******************************************************************************/
    #include

    int main()
    {
    int i,j,n,temp;
    int a[100];
    scanf(“%d”,&n);
    for(i=0;i<n;i++)
    {

    scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
    for(j=0;ja[j+1])
    {
    temp = a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
    }
    }
    }

    for(i=0;i<n;i++)
    {
    printf("%d\n",a[i]);
    }

    return 0;
    }


  • Pallipattu

    n=list(map(int,input().split(” “)))
    for i in range(len(n)):
    for j in range(i,len(n)):
    if n[i]>n[j]:
    n[i],n[j]=n[j],n[i]
    print(n)


  • Pallipattu

    n=list(map(int,input().split(” “)))
    k=[]
    for i in range(len(n)):
    a=min(n)
    k.append(a)
    b=n.remove(a)
    print(k)


  • Abhishek

    Using Cpp:
    #include
    using namespace std;
    int main(){
    int a[100], small=999999, index=0,n, temp;
    cout<>n;
    cout<<"Enter Array elements: ";
    for(int i=0;i>a[i];
    }
    for(int i=0;i<n;i++){
    small=999999, index=i;
    for(int j=i; j<n; j++){
    if(a[j]<small){
    small=a[j];
    index=j;
    }
    }
    temp=a[i];
    a[i]= small;
    a[index] = temp;
    }
    cout<<"\nSorted array: ";
    for(int i=0;i<n;i++){
    cout<<a[i]<<" ";
    }
    return 0;
    }


  • Rohan

    #include
    #include
    int main()
    {
    int n ,i ,j ,min , temp, a[100];
    printf(“Enter the size of the array\n”);
    scanf(“%d”,&n);
    printf(“Enter the array elements\n”);
    for(i=0; i<n; i++)
    {
    scanf("%d",&a[i]);
    }
    for(i=0; i<n-1; i++)
    {
    min=i;
    for(j=i+1;ja[j])
    min=j;
    }
    if(min!= i)
    {
    temp = a[i];
    a[i]=a[min] ;
    a[min] = temp;
    }
    }
    printf(“Array elements after sorting are:\n”);
    for(i=0; i<n; i++)
    {
    printf("%d\n",a[i]);
    }
    return 0;
    }


  • Priyanka

    public class Bubble_sort {
    void bubbleSort(int array[]){
    int size = array.length;

    for (int i=0; i< size -1; i++) {
    for (int j =0; j< size-i-1; j++) {
    if(array[j+1]< array[j]) {
    int temp = array[j+1];
    array[j+1]= array[j];
    array[j]= temp;}
    }
    } }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int []arr = {-2,4,5,6,7,8,-9,23};
    Bubble_sort bs = new Bubble_sort();
    bs.bubbleSort(arr);
    System.out.print(Arrays.toString(arr));

    }
    }


  • Shubham

    #include
    int selection_sort(int arr[],int arr_count){
    int temp;
    for(int i=0;i<arr_count;++i){
    for(int j=i+1;j arr[j]){
    temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    }
    }
    }

    for(int i=0;i<arr_count;i++){
    printf("%d ",arr[i]);
    }
    }
    int main()
    {
    int n;
    scanf("%d",&n);
    int arr[n];

    for(int i=0;i<n;i++){
    scanf("%d",&arr[i]);
    }

    selection_sort(arr,n);

    return 0;
    }


  • Nitish Kumar Singh

    #include
    int sort(int a[],int n)
    {
    int i,j,temp;
    for(i=0;i<n-1;i++)
    {
    for(j=i+1;ja[j])
    {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
    }
    }
    }
    }

    int main()
    {
    int i,j,a[100],temp,n;
    scanf(“%d”,&n);
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    }
    sort(a,n);
    for(i=0;i<n;i++)
    {
    printf("%d ",a[i]);
    }
    return 0;
    }