Sorting Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Sorting Arrays

Description:

Sorting Arrays. ANSI-C. Selection Sort. Assume want to sort a ... void interchange( int table[], int i, int k); void selectionSort( int table[], int length) ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 7
Provided by: defau640
Learn more at: https://www.cs.uno.edu
Category:

less

Transcript and Presenter's Notes

Title: Sorting Arrays


1
Sorting Arrays
  • ANSI-C

2
Selection Sort
  • Assume want to sort a table of integers, of size
    length, in increasing order.
  • Sketch of algorithm
  • Find position of smallest in table0..length-1,
    place it in place 0
  • Find position of smallest in table 1..length-1,
    place it in place 1
  • Find position of smallest in table
    2..length-1,, place it in place 2
  • .
  • Find position of smallest in table
    length-2..length-1, place it in place length-2
  • Find position of smallest in table
    length-1..length-1, place it in place length 1
  • General form
  • Find position of smallest in tablei..length-1
    and place it in place i
  • For i0..length-2
  • It calls for a loop

3
  • int i 0
  • while (i lt ?)
  • find position of smallest in tablei..length-1
  • place element at that position in place i
  • i i 1
  • So, we need to write two functions
  • int smallestPos ( int table, int first, int
    last)
  • And a function that interchanges the elements in
    the array in places i, that the position where
    the smallest in tablei..length-1
  • void interchange( int table, int i, int k)

4
  • void selectionSort( int table, int length)
  • int i 0
  • int smallest
  • while (i lt length - 1)
  • smallest smallestPos(table, i, length-1)
  • interchange( table, i, smallest)
  • i i 1

5
  • int smallestPos ( int table, int first, int
    last)
  • int pos first
  • int i first 1
  • while ( i lt last)
  • if (tablepos gt tablei)
  • pos i
  • i i 1
  • return pos

6
  • void interchange( int table, int i, int k)
  • int temp tablei
  • tablei tablek
  • tablek temp
Write a Comment
User Comments (0)
About PowerShow.com