SlideShare a Scribd company logo
2
Most read
5
Most read
11
Most read
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012Lecture 01: Programming Fundamentals
Lecture 10
Sorting
1
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
Sorting Arrays
• The process of arranging the values of an array
in particular order is called sorting.
• Two Types of sorting:
• Ascending Order: The smallest value is stored in the
first location of an array. The largest value is stored at
the last position of an array.
15 25 33 47 51
• Descending Order: The largest value is stored in the first
location of an array. The smallest value is stored at the
last position of an array.
51 47 33 25 15
2
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
3
Given an array of length n,
– Search elements 0 through n-1 and select the smallest
• Swap it with the element in location 0
– Search elements 1 through n-1 and select the smallest
• Swap it with the element in location 1
– Search elements 2 through n-1 and select the smallest
• Swap it with the element in location 2
– Search elements 3 through n-1 and select the smallest
• Swap it with the element in location 3
– Continue in this fashion until there’s nothing left to
search
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
4
Example and analysis of selection sort
• The selection sort might swap an
array element with itself--this is
harmless, and not worth checking
for
• Analysis:
– The outer loop executes n-1 times
– The inner loop executes about n/2
times on average (from n to 2
times)
– Work done in the inner loop is
constant (swap two array
elements)
– Time required is roughly (n-
1)*(n/2)
– You should recognize this as O(n2)
7 2 8 5 4
2 7 8 5 4
2 4 8 5 7
2 4 5 8 7
2 4 5 7 8
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
Selection Sort
Program that take 5 random values from user
in an array and sort it in ascending order.
5
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
int array[5], i, j, temp;
cout<< "Enter five integers" << endl;
for(i=0; i<5; i++)
{
cin>>array[i];
}
cout<<"The array in Actual Order n"<<endl;
for(i=0; i<5; i++)
{
cout<<array[i]<<" ";
}
for(i=0; i<5; i++)
for(j=i+1; j<5; j++)
if(array[i]>array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
cout<<endl;
cout<<"The array in Sorted Ordern ";
for(i=0; i<5; i++)
{
cout<<array[i]<<" ";
}
getch();
}
6
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
Bubble Sort
• In the bubble sort, as elements are sorted they
gradually "bubble" (or rise) to their proper
location in the array, like bubbles rising in a glass
of soda.
• The bubble sort repeatedly compares adjacent
elements of an array.
– The first and second elements are compared and swapped if out
of order.
– Then the second and third elements are compared and swapped
if out of order.
– This sorting process continues until the last two elements of the
array are compared and swapped if out of order.
7
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
• When this first pass through the array is complete,
the bubble sort returns to elements one and two
and starts the process all over again.
• So, when does it stop?
• The bubble sort knows that it is finished when it examines
the entire array and no "swaps" are needed (thus the list is
in proper order).
• The bubble sort keeps track of occurring swaps by the use
of a flag.
8
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
• The table below follows an array of numbers before,
during, and after a bubble sort for descending order.
• A "pass" is defined as one full trip through the array
comparing and if necessary, swapping, adjacent
elements.
• Several passes have to be made through the array
before it is finally sorted.
9
Array at beginning: 84 69 76 86 94 91
After Pass #1: 84 76 86 94 91 69
After Pass #2: 84 86 94 91 76 69
After Pass #3: 86 94 91 84 76 69
After Pass #4: 94 91 86 84 76 69
After Pass #5 (done): 94 91 86 84 76 69
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
Program that take 5 random values from user
in an array and sort it in ascending order,
using bubble sort.
10
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
main()
{
int array[5], i, j, temp;
cout<< "Enter five integers" << endl;
for(i=0; i<5; i++)
{
cin>>array[i];
}
cout<<"The array in Actual Order n"<<endl;
for(i=0; i<5; i++)
{
cout<<array[i]<<" ";
}
for(i=0; i<5; i++)
for(j=0; j<4; j++)
if(array[j]>array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
cout<<endl;
cout<<"The array in Sorted Ordern ";
for(i=0; i<5; i++)
{
cout<<array[i]<<" ";
}
getch();
} 11

More Related Content

PPTX
2D Array
PPTX
Bubble sort, Selection sort SORTING .pptx
PDF
03 Linear Arrays Memory Representations .pdf
PPTX
Array and string
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPTX
Procedural programming
PDF
Programming For Problem Solving Lecture Notes
PPT
User defined functions in C programmig
2D Array
Bubble sort, Selection sort SORTING .pptx
03 Linear Arrays Memory Representations .pdf
Array and string
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Procedural programming
Programming For Problem Solving Lecture Notes
User defined functions in C programmig

What's hot (20)

PPTX
sorting and searching.pptx
PDF
List,tuple,dictionary
PPTX
Stacks and Queue - Data Structures
PPTX
linked list in data structure
PPTX
sorting and its types
PPTX
Deletion from single way linked list and search
PPTX
Sparse matrix and its representation data structure
PPT
Binary Search
PPTX
Binary search in data structure
PPT
PPTX
stack & queue
PPSX
Data Structure (Queue)
PPSX
Stacks Implementation and Examples
PPTX
PPTX
Introduction to data structure ppt
PPTX
STACKS IN DATASTRUCTURE
PPTX
Datastructures in python
PPTX
Linked List
PPTX
Tree and graph
sorting and searching.pptx
List,tuple,dictionary
Stacks and Queue - Data Structures
linked list in data structure
sorting and its types
Deletion from single way linked list and search
Sparse matrix and its representation data structure
Binary Search
Binary search in data structure
stack & queue
Data Structure (Queue)
Stacks Implementation and Examples
Introduction to data structure ppt
STACKS IN DATASTRUCTURE
Datastructures in python
Linked List
Tree and graph
Ad

Similar to Array sorting (20)

PDF
Sorting Algorithms: Bubble Sort, Selection Sort,
PPT
PPT
Sorting algorithms
PDF
L 14-ct1120
PPTX
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
PPT
PPTX
Sorting Techniques in C programming
PDF
Chapter Two.pdf
PPT
search_sort Search sortSearch sortSearch sortSearch sort
PDF
Data structures arrays
PDF
Searching and Sorting in Data Structures with examples
PPTX
Data structure presentation sorting
PPTX
Searching and sorting Techniques in Data structures
PPT
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
PPT
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
PPTX
searching in data structure.pptx
PPTX
2.Problem Solving Techniques and Data Structures.pptx
PPTX
Sorting Data structure And Algorithm.pptx
PDF
CP PPT_Unit IV computer programming in c.pdf
PPTX
Different Sorting tecniques in Data Structure
Sorting Algorithms: Bubble Sort, Selection Sort,
Sorting algorithms
L 14-ct1120
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Sorting Techniques in C programming
Chapter Two.pdf
search_sort Search sortSearch sortSearch sortSearch sort
Data structures arrays
Searching and Sorting in Data Structures with examples
Data structure presentation sorting
Searching and sorting Techniques in Data structures
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
searching in data structure.pptx
2.Problem Solving Techniques and Data Structures.pptx
Sorting Data structure And Algorithm.pptx
CP PPT_Unit IV computer programming in c.pdf
Different Sorting tecniques in Data Structure
Ad

More from ALI RAZA (20)

PPTX
Structure
PPTX
Recursion
PPTX
pseudocode and Flowchart
PPTX
Algorithm Development
PPT
Programming Fundamentals using C++
PPTX
Introduction to Programming
PPTX
Introduction to Programming
PPTX
Array programs
PPTX
2D-Array
DOCX
Quiz game documentary
PPTX
Function pass by value,function pass by reference
PPTX
Drug Addiction 39 Slides
PPTX
Drug Addiction Original 51 Slides
PPTX
Passing stuctures to function
PDF
Basic general knowledge
PDF
Dil hua kirchi kirchi by mohammad iqbal shams
PDF
Pathar kar-do-ankh-mein-ansu-complete
PDF
Husne akhlaq
PDF
Parts of speech sticky note definitions and examples
PDF
Quik tips
Structure
Recursion
pseudocode and Flowchart
Algorithm Development
Programming Fundamentals using C++
Introduction to Programming
Introduction to Programming
Array programs
2D-Array
Quiz game documentary
Function pass by value,function pass by reference
Drug Addiction 39 Slides
Drug Addiction Original 51 Slides
Passing stuctures to function
Basic general knowledge
Dil hua kirchi kirchi by mohammad iqbal shams
Pathar kar-do-ankh-mein-ansu-complete
Husne akhlaq
Parts of speech sticky note definitions and examples
Quik tips

Recently uploaded (20)

PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Institutional Correction lecture only . . .
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
01-Introduction-to-Information-Management.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Microbial disease of the cardiovascular and lymphatic systems
VCE English Exam - Section C Student Revision Booklet
Institutional Correction lecture only . . .
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
RMMM.pdf make it easy to upload and study
Supply Chain Operations Speaking Notes -ICLT Program
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pre independence Education in Inndia.pdf
Microbial diseases, their pathogenesis and prophylaxis
01-Introduction-to-Information-Management.pdf
GDM (1) (1).pptx small presentation for students
Module 4: Burden of Disease Tutorial Slides S2 2025
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
102 student loan defaulters named and shamed – Is someone you know on the list?
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Final Presentation General Medicine 03-08-2024.pptx

Array sorting

  • 1. University Institute of Information Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012Lecture 01: Programming Fundamentals Lecture 10 Sorting 1
  • 2. University Institute of Information Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 Sorting Arrays • The process of arranging the values of an array in particular order is called sorting. • Two Types of sorting: • Ascending Order: The smallest value is stored in the first location of an array. The largest value is stored at the last position of an array. 15 25 33 47 51 • Descending Order: The largest value is stored in the first location of an array. The smallest value is stored at the last position of an array. 51 47 33 25 15 2
  • 3. University Institute of Information Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 3 Given an array of length n, – Search elements 0 through n-1 and select the smallest • Swap it with the element in location 0 – Search elements 1 through n-1 and select the smallest • Swap it with the element in location 1 – Search elements 2 through n-1 and select the smallest • Swap it with the element in location 2 – Search elements 3 through n-1 and select the smallest • Swap it with the element in location 3 – Continue in this fashion until there’s nothing left to search
  • 4. University Institute of Information Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 4 Example and analysis of selection sort • The selection sort might swap an array element with itself--this is harmless, and not worth checking for • Analysis: – The outer loop executes n-1 times – The inner loop executes about n/2 times on average (from n to 2 times) – Work done in the inner loop is constant (swap two array elements) – Time required is roughly (n- 1)*(n/2) – You should recognize this as O(n2) 7 2 8 5 4 2 7 8 5 4 2 4 8 5 7 2 4 5 8 7 2 4 5 7 8
  • 5. University Institute of Information Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 Selection Sort Program that take 5 random values from user in an array and sort it in ascending order. 5
  • 6. University Institute of Information Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 int array[5], i, j, temp; cout<< "Enter five integers" << endl; for(i=0; i<5; i++) { cin>>array[i]; } cout<<"The array in Actual Order n"<<endl; for(i=0; i<5; i++) { cout<<array[i]<<" "; } for(i=0; i<5; i++) for(j=i+1; j<5; j++) if(array[i]>array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } cout<<endl; cout<<"The array in Sorted Ordern "; for(i=0; i<5; i++) { cout<<array[i]<<" "; } getch(); } 6
  • 7. University Institute of Information Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 Bubble Sort • In the bubble sort, as elements are sorted they gradually "bubble" (or rise) to their proper location in the array, like bubbles rising in a glass of soda. • The bubble sort repeatedly compares adjacent elements of an array. – The first and second elements are compared and swapped if out of order. – Then the second and third elements are compared and swapped if out of order. – This sorting process continues until the last two elements of the array are compared and swapped if out of order. 7
  • 8. University Institute of Information Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 • When this first pass through the array is complete, the bubble sort returns to elements one and two and starts the process all over again. • So, when does it stop? • The bubble sort knows that it is finished when it examines the entire array and no "swaps" are needed (thus the list is in proper order). • The bubble sort keeps track of occurring swaps by the use of a flag. 8
  • 9. University Institute of Information Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 • The table below follows an array of numbers before, during, and after a bubble sort for descending order. • A "pass" is defined as one full trip through the array comparing and if necessary, swapping, adjacent elements. • Several passes have to be made through the array before it is finally sorted. 9 Array at beginning: 84 69 76 86 94 91 After Pass #1: 84 76 86 94 91 69 After Pass #2: 84 86 94 91 76 69 After Pass #3: 86 94 91 84 76 69 After Pass #4: 94 91 86 84 76 69 After Pass #5 (done): 94 91 86 84 76 69
  • 10. University Institute of Information Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 Program that take 5 random values from user in an array and sort it in ascending order, using bubble sort. 10
  • 11. University Institute of Information Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 main() { int array[5], i, j, temp; cout<< "Enter five integers" << endl; for(i=0; i<5; i++) { cin>>array[i]; } cout<<"The array in Actual Order n"<<endl; for(i=0; i<5; i++) { cout<<array[i]<<" "; } for(i=0; i<5; i++) for(j=0; j<4; j++) if(array[j]>array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } cout<<endl; cout<<"The array in Sorted Ordern "; for(i=0; i<5; i++) { cout<<array[i]<<" "; } getch(); } 11