KEMBAR78
Presentation on C Array Sorting Program.pdf
C++ Array Sorting Program
This presentation outlines a C++ program designed to sort numbers in
an array using distinct functions for reading, displaying, sorting, and
swapping elements.
Lab Experiment: 1
Introduction to the Assignment
This assignment focuses on understanding core C++ concepts such as input, output, functions, and call by reference. We
will explore how these elements work together to create an efficient sorting program.
1
Objective
Learn fundamental C++ programming concepts.
2
Key Concepts
Input, Output, Functions, Call by Reference.
Program Structure: Header and Main Function
The program begins with standard C++ includes and namespace declarations, followed by function prototypes for display, swap, and bubble sort. The main function initializes an
array and orchestrates the sorting process.
#include<iostream>using namespace std;void display(int *, int);void swapp(int &, int &);void bSort(int *,int); //bubblesortint main(){ int arr[5]; int
n=5; cout<<"Number of Elememts are:"<<n<<endl; cout << "Enter the 5 elements:" << endl; for(int i = 0; i<n; i++) { cin >> arr[i]; } cout << "Array
before Sorting: "; display(arr, n); bSort(arr, n); cout << "Array after Sorting: "; display(arr, n);}
The `swapp` Function: Call by Reference
The `swapp` function is crucial for rearranging array elements. It demonstrates the concept of "call by reference," allowing direct
modification of the original variables passed to the function.
void swapp(int &a, int &b){ int temp; temp = a;
a = b; b = temp;}
Purpose
Exchanges the values of two integer variables.
Mechanism
Uses references (`&`) to modify original variables.
Benefit
Avoids copying large data, improving efficiency.
The `display` Function:
Outputting Array Elements
The `display` function is responsible for printing the array's contents to
the console. It iterates through the array and outputs each element,
providing a clear view of the array's state before and after sorting.
void display(int *array, int size){ for(int i = 0; i<size;
i++) cout << array[i] << " "; cout << endl;}
The `bSort` Function: Bubble Sort Algorithm
The `bSort` function implements the Bubble Sort algorithm. It repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order. This process continues until the list is sorted.
void bSort(int *array, int size){ for(int i = 0; i<size; i++) { for(int j = 0; j<size-i-1; j++) {
if(array[j] > array[j+1]) swapp(array[j], array[j+1]); } }}
How Bubble Sort Works
Sorted
Repeat
Swap
Compare
Start
Each pass of the algorithm moves the largest unsorted element to its correct position at the end of the array. The outer loop
controls the number of passes, and the inner loop performs the comparisons and swaps.
Sample Output: Before and After Sorting
The program's execution demonstrates the array's transformation from an unsorted state to a sorted one, showcasing the
effectiveness of the implemented functions.
Number of Elememts are:5Enter the 5 elements:5 11 23 2 8Array before Sorting: 5 11 23 2 8Array after Sorting: 2 5 8 11 23
Key Learnings from the Experiment
This lab experiment provided practical experience with fundamental C++ programming concepts.
Function Decomposition
Breaking down a problem into
smaller, manageable functions.
Call by Reference
Understanding its importance for
efficient data manipulation.
Sorting Algorithms
Implementing and observing the
Bubble Sort in action.
Conclusion and Next Steps
This assignment successfully demonstrated the application of C++ functions and call by reference for array sorting. Future experiments could explore other sorting
algorithms or more complex data structures.
Further Exploration
• Implement Quick Sort or Merge Sort.
• Analyze time complexity of different algorithms.
• Apply sorting to real-world data sets.

Presentation on C Array Sorting Program.pdf

  • 1.
    C++ Array SortingProgram This presentation outlines a C++ program designed to sort numbers in an array using distinct functions for reading, displaying, sorting, and swapping elements.
  • 2.
    Lab Experiment: 1 Introductionto the Assignment This assignment focuses on understanding core C++ concepts such as input, output, functions, and call by reference. We will explore how these elements work together to create an efficient sorting program. 1 Objective Learn fundamental C++ programming concepts. 2 Key Concepts Input, Output, Functions, Call by Reference.
  • 3.
    Program Structure: Headerand Main Function The program begins with standard C++ includes and namespace declarations, followed by function prototypes for display, swap, and bubble sort. The main function initializes an array and orchestrates the sorting process. #include<iostream>using namespace std;void display(int *, int);void swapp(int &, int &);void bSort(int *,int); //bubblesortint main(){ int arr[5]; int n=5; cout<<"Number of Elememts are:"<<n<<endl; cout << "Enter the 5 elements:" << endl; for(int i = 0; i<n; i++) { cin >> arr[i]; } cout << "Array before Sorting: "; display(arr, n); bSort(arr, n); cout << "Array after Sorting: "; display(arr, n);}
  • 4.
    The `swapp` Function:Call by Reference The `swapp` function is crucial for rearranging array elements. It demonstrates the concept of "call by reference," allowing direct modification of the original variables passed to the function. void swapp(int &a, int &b){ int temp; temp = a; a = b; b = temp;} Purpose Exchanges the values of two integer variables. Mechanism Uses references (`&`) to modify original variables. Benefit Avoids copying large data, improving efficiency.
  • 5.
    The `display` Function: OutputtingArray Elements The `display` function is responsible for printing the array's contents to the console. It iterates through the array and outputs each element, providing a clear view of the array's state before and after sorting. void display(int *array, int size){ for(int i = 0; i<size; i++) cout << array[i] << " "; cout << endl;}
  • 6.
    The `bSort` Function:Bubble Sort Algorithm The `bSort` function implements the Bubble Sort algorithm. It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process continues until the list is sorted. void bSort(int *array, int size){ for(int i = 0; i<size; i++) { for(int j = 0; j<size-i-1; j++) { if(array[j] > array[j+1]) swapp(array[j], array[j+1]); } }}
  • 7.
    How Bubble SortWorks Sorted Repeat Swap Compare Start Each pass of the algorithm moves the largest unsorted element to its correct position at the end of the array. The outer loop controls the number of passes, and the inner loop performs the comparisons and swaps.
  • 8.
    Sample Output: Beforeand After Sorting The program's execution demonstrates the array's transformation from an unsorted state to a sorted one, showcasing the effectiveness of the implemented functions. Number of Elememts are:5Enter the 5 elements:5 11 23 2 8Array before Sorting: 5 11 23 2 8Array after Sorting: 2 5 8 11 23
  • 9.
    Key Learnings fromthe Experiment This lab experiment provided practical experience with fundamental C++ programming concepts. Function Decomposition Breaking down a problem into smaller, manageable functions. Call by Reference Understanding its importance for efficient data manipulation. Sorting Algorithms Implementing and observing the Bubble Sort in action.
  • 10.
    Conclusion and NextSteps This assignment successfully demonstrated the application of C++ functions and call by reference for array sorting. Future experiments could explore other sorting algorithms or more complex data structures. Further Exploration • Implement Quick Sort or Merge Sort. • Analyze time complexity of different algorithms. • Apply sorting to real-world data sets.