Data Structure: in This Lecture, You Will Learn About

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Data Structure

In this lecture, you will learn about :-

• Sorting algorithms
• Selection sort
• Bubble sort
C// Central Ideas:-
- Sorting algorithms
- Selection sort
Sorting - Bubble sort
Algorithms D// Objectives:-
After studying this unit, the
student will be
able to use following ways in
sorting:
- Selection sort
- Bubble sort
Definition of sorting algorithms
A sorting algorithm is a method for reorganizing a large number of items
into a specific order, such as alphabetical, highest-to-lowest value or
shortest-to-longest distance. Sorting algorithms take lists of items as
input data, perform specific operations on those lists and deliver ordered
arrays as output.

Generally, sorting algorithms are classified into two different categories:


1.Comparison sort:

Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort, Heap
Sort
2. Non-comparison sort:

Counting Sort, Radix Sort


the information does go out to people, and then it must often be
sorted in some way.

The techniques of sorting can be divided into two categories. These are:
•Internal Sorting
•External Sorting
Internal Sorting: If all the data that is to be sorted can be adjusted at a time in the
main memory, the internal sorting method is being performed.
External Sorting: When the data that is to be sorted cannot be accommodated in
the memory at the same time and some has to be kept in auxiliary memory such
as hard disk, floppy disk, magnetic tapes etc, then external sorting methods are
performed.
complexity of sorting algorithm

The complexity of sorting algorithm calculates the running time of a


function in which 'n' number of items are to be sorted. The choice for
which sorting method is suitable for a problem depends on several
dependency configurations for different problems. The most noteworthy
of these considerations are:
•The length of time spent by the programmer in programming a specific
sorting program

•Amount of machine time necessary for running the program


•The amount of memory necessary for running the program
Selection sort is a simple sorting algorithm.
This sorting algorithm is an in-place comparison-based
algorithm in which the list is divided into two parts,
the sorted part at the left end and the unsorted part
Selection at the right end. Initially, the sorted part is empty and

Sort the unsorted part is the entire list

It is similar to the way that many people do their


sorting. Selection sort is one of the O(n2) sorting
algorithms, which makes it quite inefficient for sorting
large data volumes. Selection sort is notable for its
programming simplicity and it can over perform other
sorts in certain situations (see complexity analysis for
more details).
⚫ Algorithm
1.Define the entire array as the unsorted
portion of the array
2. While the unsorted portion of the array
has more than one element:
 Find its largest element.
 Swap with last element (assuming their
values are different).
 Reduce the size of the unsorted
portion of the array by 1.
Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort.

chose the smallest & exchange


Example. Sort the following array using selection sort.

Before sorting 14 2 10 5 1 3 17 7

After pass 1 14 2 10 5 1 3 7 17

After pass 2 7 2 10 5 1 3 14 17

After pass 3 7 2 3 5 1 10 14 17

After pass 4 1 2 3 5 7 10 14 17
Number of comparisons: (n - 1) + (n - 2) + (n - 3) + ..... + 1 = n(n - 1) /
2 nearly equals to n2.

Time Complexities:

- Worst Case Complexity:


If we want to sort in ascending order and the array is in descending
order then, the worst case occurs.
-Best Case Complexity:
It occurs when the array is already sorted
-Average Case Complexity:)
It occurs when the elements of the array are in jumbled order (neither
ascending nor descending).

Space Complexity:

Space complexity is O(1) because an extra variable temp is used.

You might also like