Algorithms for Selection Sort and Bubble Sort: Implementation and Function Calls - Prof. M, Study notes of Computer Science

The implementation and function calls for selection sort and bubble sort algorithms. The selection sort algorithm finds the index of the smallest value in a given range, while bubble sort swaps adjacent elements if they are in the wrong order. Both algorithms are essential in computer science for sorting data.

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-ghb
koofers-user-ghb 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms for Selection Sort and Bubble Sort
from p 401 of textbook, modified
find and return the index of the
smallest value in the array within the
given range
int get_max_range(
char list[ ], /* the array we are
looking in */
int first, /* the index of the
first element we will
look at */
int last ) /* the index of the
last element we will
look at */
index_of_max = first
for each value of i from first+1 to last
if list[i] is greater than
list[index_of_max]
update index_of_max to i
return index_of_max
pf3

Partial preview of the text

Download Algorithms for Selection Sort and Bubble Sort: Implementation and Function Calls - Prof. M and more Study notes Computer Science in PDF only on Docsity!

Algorithms for Selection Sort and Bubble Sort from p 401 of textbook, modified find and return the index of the smallest value in the array within the given range int get_max_range( char list[ ], /* the array we are looking in / int first, / the index of the first element we will look at / int last ) / the index of the last element we will look at */ index_of_max = first for each value of i from first+1 to last if list[i] is greater than list[index_of_max] update index_of_max to i return index_of_max

void select_sort ( char list[ ],/* list to be sorted / int n ) / number of elements / int to_fill / subscript of last element of unsorted array/ char temp /temporary storage / index_of_max /index of greatest element in unsorted array */ for to_fill = n- 1 (whole array unsorted) down to to_fill = 1 (last 2 elements need to be put in order) Find position of greatest element in unsorted array. Swap it with the element at end of unsorted array.