Sorting arrays
This presentation is the property of its rightful owner.
Sponsored Links
1 / 13

Sorting Arrays PowerPoint PPT Presentation


  • 59 Views
  • Uploaded on
  • Presentation posted in: General

Sorting Arrays. 8.3 Focus on Software Engineering: Introduction to Sorting Algorithms. Sorting algorithms are used to arrange random data into some order. The Bubble Sort. An easy way to arrange data in ascending or descending order. Pseudocode: Do Set count variable to 0

Download Presentation

Sorting Arrays

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Sorting arrays

Sorting Arrays


8 3 focus on software engineering introduction to sorting algorithms

8.3 Focus on Software Engineering: Introduction to Sorting Algorithms

  • Sorting algorithms are used to arrange random data into some order


The bubble sort

The Bubble Sort

  • An easy way to arrange data in ascending or descending order.

  • Pseudocode:

    Do

    Set count variable to 0

    For count is set to each subscript in Array from 0 to the next-to-last subscript

    If array[count] is greater than array[count+1]

    swap them

    set swap flag to true

    end if

    End for

    While any elements have been swapped.


Bubble sort visual presentation

Bubble Sort Visual Presentation


Program 8 4

Program 8-4

// This program uses the bubble sort algorithm to sort an

// array in ascending order.

#include <iostream>

using namespace std;

// Function prototypes

void sortArray(int [], int);

void showArray(int [], int);

int main()

{

int values[4] = {4,5,1,2};

cout << "The unsorted values are:\n";

showArray(values, 4);

sortArray(values, 4);

cout << "The sorted values are:\n";

showArray(values, 4);

system("pause");

return 0;

}


Program continues

Program continues

// Definition of function showArray.

// This function displays the contents of array. elems is the

// number of elements.

void showArray(int array[], int elems)

{

for (int count = 0; count < elems; count++)

{

cout << array[count] << " ";

}

cout << endl;

}


Program continues1

Program continues

// Definition of function sortArray. This function performs an ascending

// order bubble sort on Array. elems is the number of elements in the array.

void sortArray(int array[], int elems)

{

int swap=1, temp;

do

{

swap = 0;

for (int count = 0; count < (elems - 1); count++)

{

if (array[count] > array[count + 1])

{

temp = array[count];

array[count] = array[count + 1];

array[count + 1] = temp;

swap = 1;

}

}

} while (swap != 0);

}


Program output

Program Output

The unsorted values are:

4 5 1 2

The sorted values are:

1 2 4 5


The selection sort

The Selection Sort

  • The bubble sort is inefficient for large arrays because items only move by one element at a time.

  • The selection sort moves items immediately to their final position in the array so it makes fewer exchanges.


Selection sort pseudocode

Selection Sort Pseudocode:

For Start is set to each subscript in Array from 0 through the next-to-last subscript

Set Index variable to Start

Set minIndex variable to Start

Set minValue variable to array[Start]

For Index is set to each subscript in Array from Start+1 through the next-to-last subscript

If array[Index] is less than minValue

Set minValue to array[Index]

Set minIndex to Index

End if

Increment Index

End For

Set array[minIndex] to array[Start]

Set array[Start] to minValue

End For


Selection sort visual presentation

Selection Sort Visual Presentation


Activity

Activity:

Make a program using selection sort algorithm to sort an arrays.

Note: You must enter 10 unsorted values of an array then display the sorted value of an arrays.


Thank you

Thank you!!! 


  • Login