16b-sort - CSE143 Lecture16(B Sorting reading:13.1,13.313.4...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 143 Lecture 16 (B) Sorting reading: 13.1, 13.3 - 13.4 slides created by Marty Stepp http://www.cs.washington.edu/143/
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Sorting sorting : Rearranging the values in an array or collection into a  specific order (usually into their "natural ordering"). one of the fundamental problems in computer science can be solved in many ways: there are many sorting algorithms some are faster/slower than others some use more/less memory than others some work better with specific kinds of data some can utilize multiple computers / processors, ... comparison-based sorting  : determining order by comparing pairs of elements: < > compareTo , …
Image of page 2
3 Sorting methods in Java The  Arrays  and  Collections  classes in  java.util  have a  static method  sort  that sorts the elements of an array/list String[] words = {"foo", "bar", "baz", "ball"}; Arrays.sort(words); System.out.println(Arrays.toString(words)); // [ball, bar, baz, foo] List<String> words2 = new ArrayList<String>(); for (String word : words) { words2.add(word); } Collections.sort(words2); System.out.println(words2); // [ball, bar, baz, foo]
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 Collections  class Method name Description binarySearch( list , value ) returns the index of the given value in a  sorted list (< 0 if not found) copy( listTo , listFrom ) copies  listFrom 's elements to  listTo emptyList() emptyMap() emptySet() returns a read-only collection of the  given type that has no elements fill( list , value ) sets every element in the list to have  the given value max( collection ) min( collection ) returns largest/smallest element replaceAll( list , old , new ) replaces an element value with another reverse( list ) reverses the order of a list's elements shuffle( list ) arranges elements into a random order sort( list ) arranges elements into ascending  order
Image of page 4
5 Sorting algorithms bogo sort : shuffle and pray
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern