473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Would someone help me with Searching and Sorting Arrays?

This is basically what I have to do. I think I have the methods correct in terms of sorting, but how do I switch it from largest to smallest?

The selectionSort method should take an array of integers as a
parameter and will sort the numbers into order from largest to smallest.
Place a println statement in the sort so every time a new maximum is
found, it is printed to the screen. Once all are sorted, the method should
print the array to the screen on the same line. Note that this method should
change the original array and because arrays are passed by reference, the
method does not need to return anything (the passed array will be changed
at the source), so the return type should be void.

The insertion method should take an integer and an array as
parameters. This method will create a new array and that will hold
the same values as the passed array, but allow for one extra space.
The integer passed to the method will be inserted into the array in the
appropriate space. This new array should be returned by the method.

Expand|Select|Wrap|Line Numbers
  1. public class SortingStuff
  2. {
  3. public SortingStuff()
  4. {
  5. }
  6.  
  7. public void selectionSort()
  8. {
  9. int min, temp;
  10. for(int outer=0; outer<list.length-1; outer++)
  11. {
  12. min=outer;
  13. for(int inner=outer+1; inner<list.length; inner++)
  14. {
  15. if(list[inner]<list[min])
  16. {
  17. min=inner;
  18. }
  19. }
  20. temp=list[outer];
  21. list[outer]=list[min];
  22. list[min]=temp;
  23. }
  24. }
  25. public void insertion()
  26. {
  27. for(int outer=1; outer<list.length;outer++)
  28. {
  29. int position=outer;
  30. int key=list[position];
  31.  
  32. while(position>0 && list[position-1] > key)
  33. {
  34. list[position]= list[position-1];
  35. position--;
  36. }
  37. list[position]=key;
  38. }
  39. }
  40.  
  41. }
  42.  
  43. import chn.util.*;
  44. public class SearchS
  45. {
  46. public static void main(String[] args)
  47. {
  48. ConsoleIO bam = new ConsoleIO();
  49. SortingStuff sortstuff = new SortingStuff();
  50. }
  51. }
  52.  
Feb 23 '11 #1
0 1044

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Alan Searle | last post by:
I have been trying to get an array to sort on a particular column and have been having problems with my main key ... array_multisort($dirlist, SORT_DESC, SORT_STRING); This function takes an...
1
by: Darryl Woodford | last post by:
Hi, I have an array of object Anag containing a string and a list of other strings (anagram dictionary). That is, some records in the array may only have two fields (anagram and the one word...
2
by: Bryce Maschino | last post by:
hello, i am trying to make a java program that will take several different arrays and search through them in a loop suchs as: for (i=0; i < 5; i++) { if (firstarray<such and such { blah blah
0
by: **Developer** | last post by:
Would someone pass this on to MS and/or check to see it the problem persists in 2005? I don't have 2005 and do not know how to pass it on. Nor do I have the time to spend learning how unless...
11
by: Paul Lautman | last post by:
I'm having some trouble understanding what is happening with some array sorting functions. In all cases, my compare function is: function compare($x, $y) { if ( $x == $y ) return 0; else if (...
2
by: zfareed | last post by:
I managed to sort a 2-dim array of lastnames but I am having trouble with printing the corresponding first names which are stored in a different 2-dim array. Here is my code for: ...
1
by: BlackJackal | last post by:
Alright here is the problem I have for homework. I understand most of it but I am not exactly sure what the problem is asking me to do or how to search the seperate arrays using the account number...
7
by: bkisa | last post by:
i m having problems with sorting arrays related to each others. i m reading the inputs from a file named input.txt and it is structured like 30 67 12 799 1 555 ... and its length i not defined...
14
by: 2005 | last post by:
Would it suppress errors? Is he trying to hide errors in his code?
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.