Showing posts with label sorting. Show all posts
Showing posts with label sorting. Show all posts

Sorting an array using recursion in java

Problem:

Write an method that sorts an array using recursion in Java

Output:

Not applicable.

Solution:

public static void sort(double[] list) {
  sort(list, list.length - 1);
  }
 
  public static void sort(double[] list, int high) {
  if (high > 1) {
  // Find the largest number and its index
  int indexOfMax = 0;
  double max = list[0];
 for (int i = 1; i <= high; i++) {
 if (list[i] > max) {
 max = list[i];
 indexOfMax = i;
 }
 }
 // Swap the largest with the last number in the list
 list[indexOfMax] = list[high];
 list[high] = max;
 // Sort the remaining list
 sort(list, high - 1);
 }
Read More

Sorting a Two-dimensional Array in Java

Problem:

In a simple java problem, create a two-dimensional array, and fill it with random numbers. Then sort it.


Solution:


import java.util.Scanner;
public class TwoDimensional
{
  public static void main (String[] args)
  {
    int[][] nums = { {6,4,8,1},

                      {64,76,1,7},

                      {43,87,23,45},

                      {100,56,233,5}};
    int temp =0;//For later use
    int j=0,k =0;
    for (int i =0;i<nums.length*nums[0].length;i++)
    {
      for (j =0;j<nums.length;j++)
      {
        for (k =0;k<nums[j].length-1;k++)
        {
          if (nums[j][k] > nums[j][k+1])
          {
            temp = nums[j][k];
            nums[j][k] = nums[j][k+1];
            nums[j][k+1] = temp;
          }
        }
        if (j != nums.length-1 && nums[j][k] > nums[j+1][0])
        {
          temp = nums[j][k];
          nums[j][k] = nums[j+1][0];
          nums[j+1][0] = temp;
        }
      }
    }
    for (int i = 0;i<nums.length;i++)
    {
      for (j =0;j<nums[i].length;j++)
      {
        System.out.print(nums[i][j] +" ");
      }
      System.out.println();
    }
  }
}
Read More

Sorting two-dimensional array in java

This exercise can be found in the book "Introduction to Java Programming - 8th edition" by Y. Daniel Liang. No copyright infringement intended.


(Sorting two-dimensional array) Write a method to sort a two-dimensional array using following header:
public static void sort(int m[][])
The method performs a primary sort on rows and a secondary sort on columns


  import java.util.Scanner;

public class SortingTwoDimensionalArray
{
  public static int[][] sort(int[][] m)
  {
    for (int i =0;i<m.length;i++)
    {
      for (int j =0;j<m.length-1;j++)
      {
      if (m[j][0] >m[j+1][0])
      {
        int temp = m[j][0];
        int temp1 = m[j][1];
        m[j][0]= m[j+1][0];
        m[j][1]= m[j+1][1];
        m[j+1][0] = temp;
        m[j+1][1] = temp1;
      }
      
      if (m[j][0] == m[j+1][0])
      {
        if(m[j][1] >m[j+1][1])
        {
          int temp3 = m[j][1];
          m[j][1] = m[j+1][1];
          m[j+1][1] = temp3;
        }
      }

      }
    }
    return m;
  }
  //Just a test
  public static void main (String[] args)
  {
    int[][] m = { {4,2},{1,7},{4,5},{1,2},{1,1},{4,1}};
    sort(m);
    for (int i =0;i<m.length;i++)
    {
      for (int j =0;j<m[i].length;j++)
      {
        System.out.print(m[i][j] + " ");  
      }
      System.out.println();
    }
  }
}
Read More

Computing the Weekly Hours for Each Employee in Java

Problem:

Suppose the weekly hours for all employees are stored in a two-dimensional array. Each row records an employee’s seven-day work hours with seven columns. For example, the array shown below stores the work hours for eight employees. Write a program that displays employees and their total hours in decreasing order of the total hours.


Output:

2 3 4 4 5 8 8
7 3 4 3 3 4 4
3 3 4 3 3 2 2
9 3 4 7 3 4 1
3 5 4 3 6 3 8
3 4 4 6 3 4 4
3 7 4 8 3 8 4
6 3 5 9 2 7 9

Employee's 7 worked the most with 41 hours
Employee's 6 worked the most with 37 hours
Employee's 0 worked the most with 34 hours
Employee's 4 worked the most with 32 hours
Employee's 3 worked the most with 31 hours
Employee's 1 worked the most with 28 hours
Employee's 5 worked the most with 28 hours
Employee's 2 worked the most with 20 hours


Solution:

import java.util.Scanner;
public class WeeklyHour
{
  public static int[][] readHours()
  {
    Scanner scan = new Scanner (System.in);
    int[][] employees = new int[8][7];
    for (int i =0;i<employees.length;i++)
    {
      for (int j =0;j<employees[i].length;j++)
      {
        employees[i][j] = scan.nextInt();
      }
    }
    return employees;
  }
  public static int[][] sortEmployees(int[][] employees)
  {
    int[][] tempar = new int[8][2];
    for (int i =0;i<employees.length;i++)
    {
      tempar[i][0] =i;
      for (int j=0;j<employees[i].length;j++)
      {
        tempar[i][1] += employees[i][j]; 
      }
    }
    for (int i =0;i<employees.length;i++)
    {
      for (int j =0;j<employees.length -1;j++)
      {
        if ( tempar[j][1] < tempar[j+1][1] )
        {
          int temp = tempar[j][1];
          int temp2 = tempar[j][0];
          
          tempar[j][1] = tempar[j+1][1];
          tempar[j][0] = tempar[j+1][0];
          tempar[j+1][1] = temp;
          tempar[j+1][0] = temp2;
        }
      }
    }
    return tempar;
  }
  
  public static void printEmployees(int[][] tempar)
  {
    for (int i =0;i<tempar.length;i++)
    {
        System.out.println("Employee's " + tempar[i][0] +
                    " worked the most with " + tempar[i][1] + " hours" );
    }
  }
  
  public static void main (String[] args)
  {
    int[][] nums = readHours();
    int[][] nums1 = sortEmployees(nums);
    printEmployees(nums1);
  }
}
Read More

Sorting an Integer Array in Java

Problem:

Write a program that reads 10 value of integers from the user, store them in an array of integers. Then print the reverse of this array. Later, print a sorted version of the user. You are not allowed to use the Arrays.sort() method.


Output:

Enter the values: 1 6 3 4 2 9 8 10 5 7
Reversed array: 7 5 10 8 9 2 4 3 6 1
Values sorted: 1 2 3 4 5 6 7 8 9 10
-2


Solution:

import java.util.Scanner;
 
public class Problem2
{
 public static void main (String[] args)
 {
 Scanner scan = new Scanner(System.in);
 System.out.print("Enter the values: ");
 int[]list = new int[10];
 for (int i=0; i <10; i++)
 {
  list[i]= scan.nextInt();
 }
 System.out.print("Reversed array:  " );
 for(int k =9; k>=0;k--)
  System.out.print(list[k] + " ");
 
 for (int d=0;d<10;d++)
 {
  for (int i =0; i<9;i++)
  {
   if(list[i]>list[i+1])
   {
   int greater = list[i];
   list[i] = list[i+1];
   list[1+i]= greater;
   }
  }
 }
 System.out.println();
 System.out.print("Values sorted:   ");
 for(int values :  list)
  System.out.print(values + " ");
}
}
Read More

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact