Computer Science – Programming Methodologies & Data Structures
B.Sc. First Year | Major/Minor
कंप्यूटर विज्ञान – प्रोग्रामिंग पद्धतियां और डेटा संरचनाएं
बी.एससी. प्रथम वर्ष | मेजर/माइनर
A variable in C++ is a named storage location in memory that holds a value of a specific data type. It is declared with: data_type variable_name;
Example: int age = 20; float salary = 35000.5; char grade = 'A';
Variables must be declared before use and their values can change during program execution.
A structure in C++ is a user-defined data type that groups variables of different data types under one name.
Syntax:struct Student {
int rollNo;
string name;
float marks;
};
Structures allow related data to be stored together, e.g., a student's record with roll number, name, and marks.
A Linked List is a linear data structure where elements (nodes) are stored in non-contiguous memory locations. Each node contains:
1. Data – actual value
2. Pointer/Link – address of next node
Types: Singly linked list, Doubly linked list, Circular linked list.
Advantages: Dynamic size, easy insertion/deletion. Disadvantages: No random access, extra memory for pointers.
Recursion is a programming technique where a function calls itself to solve a problem by breaking it into smaller subproblems. Every recursive function needs:
1. Base case – stopping condition
2. Recursive case – function calling itself
Example: Factorial: int fact(n) { if(n==0) return 1; else return n*fact(n-1); }
Used in: Tree traversal, Tower of Hanoi, Fibonacci series.
The switch statement evaluates an expression and executes code based on matching case labels:
switch(expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// default code
}- Expression must be integer or character type.
- break prevents fall-through to next case.
- default executes if no case matches.
Example: Menu-driven programs where user selects an option (1–5) and appropriate action is performed.
A Stack is a linear data structure that follows LIFO (Last In, First Out) principle – the last element inserted is the first to be removed.
Operations:
- Push: Insert element at top of stack.
- Pop: Remove element from top of stack.
- Peek/Top: View top element without removing.
- isEmpty: Check if stack is empty.
- isFull: Check if stack is full (in array implementation).
Applications:
- Function call management (call stack).
- Undo/Redo operations in text editors.
- Expression evaluation (infix to postfix).
- Browser back-button history.
#include <iostream>
using namespace std;
int main() {
// For loop: Print 1 to 5
cout << "For Loop: ";
for(int i=1; i<=5; i++) {
cout << i << " ";
}
cout << endl;
// While loop: Print 1 to 5
cout << "While Loop: ";
int j = 1;
while(j <= 5) {
cout << j << " ";
j++;
}
return 0;
}Output: For Loop: 1 2 3 4 5 | While Loop: 1 2 3 4 5
C++ provides fstream library for file handling:
- ofstream: Write to file
- ifstream: Read from file
- fstream: Both read and write
// Writing to file
#include <fstream>
ofstream myfile;
myfile.open("data.txt");
myfile << "Hello World!";
myfile.close();
// Reading from file
ifstream infile("data.txt");
string line;
while(getline(infile, line)) { cout << line; }
infile.close();Hashing is a technique to map data (keys) to fixed-size array positions (hash table) using a hash function.
Hash Function: h(key) = key mod table_size
Example: Table size = 7. Keys: 23, 10, 45
h(23)=23%7=2, h(10)=10%7=3, h(45)=45%7=3 (collision!)
Collision Resolution:
- Chaining: Store colliding keys in linked list at same slot.
- Open Addressing: Linear probing – find next empty slot.
Applications:
- Database indexing, password storage (SHA hashing), symbol tables in compilers.
Open-source programming languages are those whose source code, tools, and compilers are freely available for use, modification, and distribution without licensing fees.
Examples:
- Python: Versatile, used in AI/ML, web development, data science.
- Java: Platform-independent (JVM), enterprise applications.
- C/C++: Systems programming, embedded systems.
- JavaScript: Web development (client and server side).
- PHP: Server-side web scripting.
- R: Statistical computing and graphics.
- Ruby: Web applications (Rails framework).
Dr. Sartaj Sahni is a renowned computer scientist and professor at University of Florida.
Key Contributions:
- Co-authored the seminal textbook "Fundamentals of Data Structures in C++" and "Fundamentals of Computer Algorithms" (with Horowitz and Anderson-Freed).
- Research in algorithm design, combinatorial algorithms, scheduling, and parallel computing.
- Pioneered work on branch and bound algorithms and approximation algorithms.
- Contributed to VLSI design and routing algorithms.
- Fellow of IEEE and ACM, recipient of numerous awards.
An Array is a collection of elements of the same data type stored in contiguous memory locations, accessed by an index.
Declaration: int arr[5] = {1,2,3,4,5};
Types of Arrays:
- 1D Array (Linear Array): Single row of elements. E.g.,
int a[5];Used for lists, stacks, queues. - 2D Array (Matrix): Rows and columns. E.g.,
int mat[3][3];Used for matrices, tables, game boards. - 3D Array: Extension to 3 dimensions. E.g.,
int cube[2][3][4];Used in image processing. - Jagged Array: Array of arrays with different sizes. Each row can have different number of columns.
- String Array: Array of character strings. E.g.,
char names[5][20];
Operations on Arrays:
- Traversal, Insertion, Deletion, Searching (linear/binary), Sorting (bubble, selection, merge sort).
Operators in C++:
- Arithmetic: +, -, *, /, % →
a+b, a%b - Relational: ==, !=, <, >, <=, >= →
a>b - Logical: && (AND), || (OR), ! (NOT) →
a>0 && b>0 - Assignment: =, +=, -=, *=, /= →
a += 5 - Increment/Decrement: ++, -- →
i++, --j - Bitwise: &, |, ^, ~, <<, >> →
a & b - Conditional (Ternary): ? : →
max = (a>b) ? a : b - sizeof: Returns size in bytes →
sizeof(int) - Scope resolution: :: → access global variable or class member
Graph is a non-linear data structure consisting of vertices (nodes) and edges (connections).
Types of Graphs:
- Directed (Digraph): Edges have direction. E.g., social network following.
- Undirected: Edges have no direction. E.g., friendship network.
- Weighted: Edges have weights (costs). E.g., road distances.
- Connected: Path exists between every pair of vertices.
- Cyclic/Acyclic: Contains/does not contain cycles.
BFS (Breadth First Search):
- Visits all neighbors of a node before going deeper.
- Uses a Queue. Time complexity: O(V+E).
- Applications: Shortest path (unweighted), social network levels.
DFS (Depth First Search):
- Explores as far as possible along each branch before backtracking.
- Uses Stack (or recursion). Time complexity: O(V+E).
- Applications: Topological sort, cycle detection, maze solving.
Sorting Algorithms:
- Bubble Sort: Compare adjacent elements; swap if out of order. O(n²). Simple but slow.
- Selection Sort: Find minimum and place in sorted position. O(n²). Simple, fewer swaps.
- Insertion Sort: Insert elements one by one into correct position. O(n²) worst, O(n) best (nearly sorted). Used for small datasets.
- Merge Sort: Divide-and-conquer. Split, sort halves, merge. O(n log n). Stable sort, used in external sorting.
- Quick Sort: Pick pivot, partition around it, recursively sort. O(n log n) average, O(n²) worst. Fastest in practice.
- Heap Sort: Uses max-heap data structure. O(n log n). In-place, not stable.
- Counting Sort: Non-comparison sort. O(n+k). Used when range of values is known.
C++ में चर (variable) मेमोरी में एक नामांकित भंडारण स्थान है जो किसी विशिष्ट डेटा प्रकार का मान रखता है।
घोषणा: data_type variable_name;
उदाहरण: int age = 20; float salary = 35000.5;
उपयोग से पहले घोषित करना अनिवार्य है।
C++ में structure एक उपयोगकर्ता-परिभाषित डेटा प्रकार है जो एक नाम के अंतर्गत विभिन्न डेटा प्रकारों के चरों को समूहित करता है।
यह संबंधित डेटा को एक साथ संग्रहीत करने देता है।
लिंक्ड लिस्ट एक रैखिक डेटा संरचना है जहां तत्व (नोड) असन्निकट मेमोरी स्थानों में संग्रहीत होते हैं। प्रत्येक नोड में: (1) डेटा, (2) अगले नोड का पॉइंटर।
प्रकार: एकल, द्विगुण, वृत्तीय।
लाभ: गतिशील आकार, सम्मिलन/हटाना आसान।
पुनरावर्तन (Recursion) एक प्रोग्रामिंग तकनीक है जहां एक फ़ंक्शन स्वयं को कॉल करके समस्या को छोटे उपसमस्याओं में तोड़कर हल करता है। इसके लिए आवश्यक: (1) आधार स्थिति, (2) पुनरावर्ती स्थिति।
उपयोग: ट्री ट्रैवर्सल, हनोई टावर, फिबोनाची।
switch स्टेटमेंट एक expression का मूल्यांकन करता है और मिलान case के आधार पर कोड चलाता है:switch(expr) { case 1: ...; break; case 2: ...; break; default: ...; }
- break बिना fall-through होता है।
- default तब चलता है जब कोई case मैच न हो।
स्टैक एक रैखिक डेटा संरचना है जो LIFO (Last In, First Out) सिद्धांत पर काम करती है।
ऑपरेशन:
- Push: शीर्ष पर तत्व डालना।
- Pop: शीर्ष से तत्व हटाना।
- Peek: शीर्ष तत्व देखना।
- isEmpty: खाली जांचना।
अनुप्रयोग:
- फ़ंक्शन कॉल प्रबंधन, Undo/Redo, ब्राउज़र बैक बटन।
#include <iostream>
using namespace std;
int main() {
// For लूप: 1 से 5 प्रिंट
for(int i=1; i<=5; i++) { cout << i << " "; }
// While लूप: 1 से 5 प्रिंट
int j=1;
while(j<=5) { cout << j << " "; j++; }
return 0;
}C++ में फाइल हैंडलिंग के लिए fstream लाइब्रेरी:
- ofstream: लिखना
- ifstream: पढ़ना
- fstream: दोनों
फाइल ऑपरेशन: open(), close(), read(), write(), getline()।
हैशिंग एक तकनीक है जो हैश फ़ंक्शन का उपयोग करके डेटा (कुंजी) को हैश टेबल में निश्चित आकार के सरणी स्थानों पर मैप करती है।
उदाहरण: h(key) = key % 7; h(23)=2, h(10)=3, h(45)=3 (टक्कर!)
टक्कर समाधान:
- चेनिंग: लिंक्ड लिस्ट में।
- ओपन एड्रेसिंग: अगला खाली स्लॉट।
ओपन-सोर्स प्रोग्रामिंग भाषाएं वे हैं जिनका स्रोत कोड, उपकरण और कंपाइलर बिना लाइसेंस शुल्क के उपयोग, संशोधन और वितरण के लिए स्वतंत्र रूप से उपलब्ध हैं।
उदाहरण:
- Python: AI/ML, वेब विकास, डेटा विज्ञान।
- Java: प्लेटफॉर्म-स्वतंत्र, एंटरप्राइज़ अनुप्रयोग।
- C/C++: सिस्टम प्रोग्रामिंग।
- JavaScript: वेब विकास।
डॉ. सरताज साहनी फ्लोरिडा विश्वविद्यालय में प्रसिद्ध कंप्यूटर वैज्ञानिक और प्रोफेसर हैं।
प्रमुख योगदान:
- "Fundamentals of Data Structures in C++" पाठ्यपुस्तक के सह-लेखक।
- एल्गोरिदम डिज़ाइन, शेड्यूलिंग, समानांतर कंप्यूटिंग में शोध।
- Branch and bound एल्गोरिदम में अग्रणी कार्य।
- IEEE और ACM के फेलो।
ऐरे समान डेटा प्रकार के तत्वों का संग्रह है जो सन्निकट मेमोरी स्थानों में संग्रहीत और इंडेक्स द्वारा एक्सेस किए जाते हैं।
ऐरे के प्रकार:
- 1D ऐरे: तत्वों की एकल पंक्ति।
- 2D ऐरे: पंक्तियां और स्तंभ (मैट्रिक्स)।
- 3D ऐरे: 3 आयामों तक विस्तार।
- जैग्ड ऐरे: अलग-अलग आकार की ऐरे।
- स्ट्रिंग ऐरे: वर्ण स्ट्रिंग की ऐरे।
C++ में ऑपरेटर:
- अंकगणितीय: +, -, *, /, %
- संबंधात्मक: ==, !=, <, >, <=, >=
- तार्किक: && (AND), || (OR), ! (NOT)
- असाइनमेंट: =, +=, -=
- वृद्धि/कमी: ++, --
- बिटवाइज़: &, |, ^, ~, <<, >>
- टर्नरी: ?: → max=(a>b)?a:b
ग्राफ एक अरैखिक डेटा संरचना है जिसमें शीर्ष (vertices) और किनारे (edges) होते हैं।
ग्राफ के प्रकार:
- निर्देशित: किनारों की दिशा होती है।
- अनिर्देशित: दिशा नहीं।
- भारित: किनारों पर वज़न।
BFS:
- Queue उपयोग करता है। पहले सभी पड़ोसियों को देखता है।
- अनुप्रयोग: सबसे छोटा रास्ता।
DFS:
- Stack या recursion। जितना हो सके गहराई में जाता है।
- अनुप्रयोग: टोपोलॉजिकल सॉर्ट, चक्र पहचान।
क्रमबद्धता एल्गोरिदम:
- बबल सॉर्ट: आसन्न तत्वों की तुलना; O(n²)।
- सिलेक्शन सॉर्ट: न्यूनतम खोजकर क्रमबद्ध स्थान पर; O(n²)।
- इंसर्शन सॉर्ट: सही स्थान पर डालना; O(n²)।
- मर्ज सॉर्ट: विभाजन-और-जीत; O(n log n)।
- क्विक सॉर्ट: पिवट चुनकर विभाजन; O(n log n) औसत।
- हीप सॉर्ट: max-heap उपयोग; O(n log n)।