TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How to track index of unsorted array after sorting that array?

1 pointsby shivajikobardan7 months ago
<p><pre><code> ``` import java.util.\*; public class Example { public static void main(String[] args) { int[][] hours = { {2, 4, 3, 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} }; int[] weeklyHrs = new int[hours.length]; for (int i = 0; i &lt; hours.length; i++) { for (int j = 0; j &lt; hours[i].length; j++) { weeklyHrs[i] = weeklyHrs[i] + hours[i][j]; } } for (int i = 0; i &lt; weeklyHrs.length; i++) { System.out.print(&quot;Employee&quot; + i + &quot;:&quot; + weeklyHrs[i]); System.out.println(); } System.out.println(); int[][] wHTwoD = new int[8][2]; for (int i = 0; i &lt; 8; i++) { for (int j = 0; j &lt; 2; j++) { if (j % 2 == 0) { wHTwoD[i][j] = i; } else { wHTwoD[i][j] = weeklyHrs[i]; } } } for (int i = 0; i &lt; 8; i++) { for (int j = 0; j &lt; 2; j++) { System.out.print(wHTwoD[i][j]); } System.out.println(); } for (int i = 0; i &lt; weeklyHrs.length; i++) { System.out.print(weeklyHrs[i] + &quot; &quot;); } } public static void sort(int[] arr) { int temp; for (int i = 0; i &lt; arr.length; i++) { for (int j = i; j &lt; arr.length; j++) { if (arr[j] &lt; arr[i]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } } } ``` # Question Suppose the weekly hours for all employees are stored in a two-dimensional array. Each row records an employee&#x27;s seven-day work hours with seven columns. For example, the following array stores the work hours for eight employees. Write a program that displays employees and their total hours in increasing order of the total hours. ![image|304x214](upload:&#x2F;&#x2F;26QTnP5J4bcdJhLotm09GZb5rXP.png) I got the total weekly hours of each employees in a 1d array like this ``` int[] weeklyHrs = new int[hours.length]; for (int i = 0; i &lt; hours.length; i++) { for (int j = 0; j &lt; hours[i].length; j++) { weeklyHrs[i] = weeklyHrs[i] + hours[i][j]; } } ``` However, now I need to sort it and I need to track the index of unsorted array even after sorting it. So far, the book hasn&#x27;t started on how to sort 2d array. So I am not going to use this as I take this as a kind of drill. My concern is simple: How do I track the index of unsorted array after sorting that array?</code></pre>

1 comment

gregjor7 months ago
Question belongs on StackOverflow, or more likely in a school study group.<p>Choose a better data structure.<p>Don&#x27;t write your own sort function. Use the features of the language and standard library. Your code is about 3x longer than necessary.