Showing posts with label Ascending Order. Show all posts
Showing posts with label Ascending Order. Show all posts

Tuesday, 13 January 2015

Cpp Program to arrange all the elements of an array in ascending order


This is a Cpp Program which arranges all the elements in an array in ascending order.

Here initially first element in array and  its next element are compared array[0] and array[1] , if array[1] is greater then the values are swapped, and the process continues with 2 and 3 elements and son on until the end of array is reached.

Finally the obtained array is the sorted array in ascending order.

PROGRAM:
#include <iostream>

using namespace std;

int main()
{
    int i,j,no,temp;

    cout << "\nEnter the no of Elements: \t";
    cin >> no;
    int arr[no];
    for(i=0;i<no;i++)
    {
        cout << "\nEnter Element " <<  i+1 << ": \t";
        cin >> arr[i];
    }

    for(i=0;i<no;i++)
    {
        for(j=i;j<no;j++)
        {
            if(arr[i] > arr[j])
           {
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
    }

    cout << ("\nSorted array: \n");
    for(i=0;i<no;i++)
    {
    cout << "\t" << arr[i];
    }
    return 0;
}

OUTPUT :
Cpp - Ascending Order

Thursday, 8 January 2015

Java Program to sort all the elements in an array in Ascending Order

This is a Java Program which arranges all the elements in an array in ascending order

Here initially first element in array and  its next element are compared array[0] and array[1] , if array[1] is greater then the values are swapped, and the process continues with 2 and 3 elements and son on until the end of array is reached.

Finally the obtained array is the sorted array in ascending order.

PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class AscendingOrder {
 public static void main(String[] args) {

  int i, j, no, temp;
  Scanner scan = new Scanner(System.in);
  System.out.print("\nEnter the number of Elements: \t");
  no = scan.nextInt();
  int arr[] = new int[no];
  for (i = 0; i < no; i++) {
   System.out.print("\nEnter Element" + (i + 1));
   arr[i] = scan.nextInt();
  }
  scan.close();
  for (i = 0; i < no; i++) {
   for (j = i; j < no; j++) {
    if (arr[i] > arr[j]) {
     temp = arr[i];
     arr[i] = arr[j];
     arr[j] = temp;
    }
   }
  }

  System.out.print("\nSorted array: ");
  for (i = 0; i < no; i++) {
   System.out.print("\t" + arr[i]);
  }
 }
}

OUTPUT :
Java - Sorting elements in an array in ascending order

C Program to arrange the elements in an array in Ascending Order


This is a C Program which arranges all the elements in an array in ascending order

Here initially first element in array and  its next element are compared array[0] and array[1] , if array[1] is greater then the values are swapped, and the process continues with 2 and 3 elements and son on until the end of array is reached.

Finally the obtained array is the sorted array in ascending order.

PROGRAM :

#include<stdio.h>
#include<conio.h>
int main()
{
    int i,j,no,temp;

    printf("\nEnter the no of Elements: \t");
    scanf("%d", &no);
    int arr[no];
    for(i=0;i<no;i++)
    {
        printf("\nEnter Element %d: ", i+1);
        scanf("%d",&arr[i]);
    }

    for(i=0;i<no;i++)
    {
        for(j=i;j<no;j++)
        {
            if(arr[i] > arr[j])
     {
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
    }

    printf("\nSorted array: ");
    for(i=0;i<no;i++)
    {
        printf("\t%d",arr[i]);
    }

    return 0;
}

OUTPUT :
C - Ascending Order
Related Programs :