ECE 242 Spring 2003 Data Structures in Java - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

ECE 242 Spring 2003 Data Structures in Java

Description:

ECE242 Spring 2003. Data Structures in Java, Prof. Gao. 1. ECE ... In BlockBuster video store, more than 1,000 movies. where is 'Matrix'? ECE242 Spring 2003 ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 22
Provided by: jxia
Category:

less

Transcript and Presenter's Notes

Title: ECE 242 Spring 2003 Data Structures in Java


1
ECE 242 Spring 2003Data Structures in Java
  • http//rio.ecs.umass.edu/ece242
  • Elementary Sorting Algorithms
  • Prof. Lixin Gao

2
Todays Topics
  • Selection Sorting
  • Insertion Sorting

3
Sorted Listing
  • Phone book
  • sorted by name
  • Course list in registration Guide
  • sorted by department
  • sorted by course number
  • ECE242 roster list
  • sorted by last name

4
Why Sorting?
  • Without sorting
  • how can I find your phone number quickly?
  • where is ECE242 course schedule?
  • In BlockBuster video store, more than 1,000
    movies
  • where is Matrix?

5
Sorting
  • Sorting by keys
  • Easy to find what you want
  • Easy to manage these stuff.

6
Sorting Integers
How to sort the integers in this array?
20
8
5
10
7
5
7
8
10
20
7
Elementary Sorting Algorithms
  • Selection Sort
  • Insertion Sort
  • Bubble Sort

8
Selection Sorting
  • Main idea
  • find the smallest element
  • put it in the first position
  • find the next smallest element
  • put it in the second position

9
Pseudo-code for Selection Sorting
  • Step
  • 1. select the smallest element
  • among datai datadata.length-1
  • 2. swap it with datai
  • 3. if not finishing, repeat 12

10
Swap Action (SelectionSorting)
20
8
5
10
7
5
8
20
10
7
5
7
20
10
8
5
7
8
10
20
5
7
8
10
20
11
SelectionSort
  • SelectionSort(int array)
  • for( i0 iltarray.length-1 i )
  • // find the minimum value index
  • // among arrayi, arrayi1,
    arraylength-1
  • int minIndex i
  • for( int ji1 jltarray.length-1 j )
  • if( arrayj lt arrayminIndex)
  • minIndex j

12
SelectionSort (continued)
  • //swap arrayminIndex with arrayi
  • int temp arrayminIndex
  • arrayminIndex arrayi
  • arrayi temp

13
Complete Example
  • SelectionSorting.java
  • int array new int 5
  • // initialize the value
  • array0 20 array1 8
  • array2 5 array3 10
  • array4 7
  • // call SelectionSort method
  • SelectionSort(array)

14
Insertion Sorting
  • Main Idea
  • Starts by considering the first two elements of
    the array data, if out of order, swap them
  • Consider the third element, insert it into the
    proper position among the first three elements.
  • Consider the forth element, insert it into the
    proper position among the first four elements.

15
Pseudo-code for Insertion Sorting
  • Place ith item in proper position
  • temp datai
  • shift those elements dataj which greater than
    temp to right by one position
  • place temp in its proper position

16
Insert Action i1
temp
20
8
5
10
7
8
i 1, first iteration
20
20
5
10
7
8
8
20
5
10
7
---
17
Insert Action i2
temp
8
20
5
10
7
5
i 2, second iteration
8
20
20
10
7
5
8
8
20
10
7
5
5
8
20
10
7
---
18
Insert Action i3
temp
5
8
20
10
7
10
i 3, third iteration
5
8
20
20
7
10
5
8
10
20
7
---
19
Insert Action i4
temp
5
8
10
20
7
7
i 4, forth iteration
5
8
10
20
20
7
5
8
10
10
20
7
5
8
8
10
20
7
5
7
8
10
20
---
20
InsertionSort
  • InsertionSort(int array)
  • int temp, i, j
  • for( i1 iltarray.length i )
  • temp arrayi
  • // assuming array0 arrayi-1 are sorted
  • for( ji jgt0 templtarrayj-1 j--)
  • arrayj arrayj-1
  • // move temp in the correct position
  • arrayj temp

21
Complete Example
  • InsertionSorting.java
  • InsertSortingInput.txt (sample input data)
  • int array new int 5
  • // initialize the value
  • inputArray()
  • // call InsertionSort method
  • InsertionSort(array)
Write a Comment
User Comments (0)
About PowerShow.com