mid low high 2 0 return mid else 0

Mid low high 2 0 return mid else 0

This preview shows page 2 - 3 out of 3 pages.

mid = (low + high) /2; if(list[mid].compareTo(key) == 0) return mid; else if(list[mid].compareTo(key) < 0){ low = mid + 1; } else{ high = mid - 1; } } return -1; } //sort: Receives a generic arrayList and returns nothing. public <E extends Comparable<E>> void sort(ArrayList<E> list) { //selection sort int minIdx ; for(int i = 0 ; i < list.size(); i++){ minIdx = i; for(int j = i + 1; j < list.size(); j++){ if(list.get(j).compareTo(list.get(minIdx)) < 0) minIdx = j; } if(minIdx != i){ E temp = list.get(i); list.set(i, list.get(minIdx)); list.set(minIdx, temp); } } } //sort: Receives a generic one-dimensional array and returns nothing. public <E extends Comparable<E>> void sort(E[] list) { //selection sort int minIdx ; for(int i = 0 ; i < list.length; i++){ minIdx = i; for(int j = i + 1; j < list.length; j++){ if(list[j].compareTo(list[minIdx]) < 0) minIdx = j; } if(minIdx != i){ E temp = list[i]; list[i] = list[minIdx]; list[minIdx] = temp; } } } //**************************************************************************** //displayOneDList: Receives a generic one-dimensional array and displays its contents
Image of page 2
//**************************************************************************** public <E> void displayOneDList(E[] list, String listName){ System.out.println("The contents of the one dimensional list " + listName + " is "); for(int i = 0; i < list.length; i++) System.out.println(list[i].toString()); } //**************************************************************************** //displayArrayList: Receives a generic arraylist & a string name // and displays its contents //**************************************************************************** public <E> void displayArrayList(ArrayList <E> list, String listName){ System.out.println("The contents of the ArrayList " + listName + " is "); for(int i = 0; i < list.size(); i++) System.out.println(list.get(i).toString()); } }
Image of page 3

You've reached the end of your free preview.

Want to read all 3 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture