DSA क्या है
In this page:
What is DSA
DSA का मतलब है Data Structures and Algorithms: वे दो halves जिनसे programs information handle करते हैं। Data structures तय करते हैं कि आप data कैसे organize और store करते हैं, जबकि algorithms step-by-step logic हैं जो इस पर काम करता है।
एक साथ ये हर efficient program के पीछे का toolkit हैं, एक search engine से लेकर एक food-delivery app के route planner तक।
उदाहरण: What is DSA
#include <iostream>
using namespace std;
int main() {
int data[] = {2, 4, 6, 8};
int sum = 0;
for (int i = 0; i < 4; i++) sum += data[i];
cout << "Sum: " << sum << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
int[] data = {2, 4, 6, 8};
int sum = 0;
for (int i = 0; i < 4; i++) sum += data[i];
System.out.println("Sum: " + sum);
}
}
data = [2, 4, 6, 8]
total = 0
for x in data:
total += x
print("Sum:", total)
#include <stdio.h>
int main() {
int data[] = {2, 4, 6, 8};
int sum = 0;
for (int i = 0; i < 4; i++) sum += data[i];
printf("Sum: %d\n", sum);
return 0;
}
Login to try C/C++/Java code in the editor
Data Structures
एक data structure बस data रखने का एक organized तरीका है ताकि specific operations तेज़ हों। एक array fixed-order access के लिए बढ़िया है, एक linked list cheap insertions के लिए, एक hash map near-instant lookups के लिए।
काम के लिए सही चुनना अक्सर एक program के scale होने और न होने के बीच का अंतर है।
उदाहरण: Data Structures
#include <iostream>
using namespace std;
int main() {
int marks[] = {90, 85, 78};
cout << "Stored value marks[0]: " << marks[0] << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
int[] marks = {90, 85, 78};
System.out.println("Stored value marks[0]: " + marks[0]);
}
}
marks = [90, 85, 78]
print("Stored value marks[0]:", marks[0])
#include <stdio.h>
int main() {
int marks[] = {90, 85, 78};
printf("Stored value marks[0]: %d\n", marks[0]);
return 0;
}
Login to try C/C++/Java code in the editor
Algorithms
एक algorithm steps का एक precise, finite sequence है जो एक input को एक correct output में बदलता है, किसी problem को solve करने के लिए एक recipe जैसा। वही problem आमतौर पर कई अलग-अलग algorithms से solve हो सकती है, और उनकी speed और memory use compare करना algorithm analysis के बारे में है।
उदाहरण: Algorithms
#include <iostream>
using namespace std;
int main() {
int nums[] = {4, 9, 2, 7};
int maxVal = nums[0];
for (int i = 1; i < 4; i++)
if (nums[i] > maxVal) maxVal = nums[i];
cout << "Max: " << maxVal << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
int[] nums = {4, 9, 2, 7};
int maxVal = nums[0];
for (int i = 1; i < 4; i++)
if (nums[i] > maxVal) maxVal = nums[i];
System.out.println("Max: " + maxVal);
}
}
nums = [4, 9, 2, 7]
max_val = nums[0]
for x in nums[1:]:
if x > max_val:
max_val = x
print("Max:", max_val)
#include <stdio.h>
int main() {
int nums[] = {4, 9, 2, 7};
int maxVal = nums[0];
for (int i = 1; i < 4; i++)
if (nums[i] > maxVal) maxVal = nums[i];
printf("Max: %d\n", maxVal);
return 0;
}
Login to try C/C++/Java code in the editor
Why DSA Matters
DSA के बिना, programs छोटे inputs पर ठीक काम करते हैं और फिर data बढ़ने पर रुकने लगते हैं, क्योंकि naive approaches अक्सर अनावश्यक रूप से काम दोहराते हैं। DSA समझना आपको ऐसी inefficiency जल्दी देखने देता है और एक approach चुनने देता है जो scale पर भी तेज़ रहे, यही कारण है कि यह technical interviews के लिए भी central है।
उदाहरण: Why DSA Matters
#include <iostream>
using namespace std;
int main() {
int nums[] = {1, 3, 5, 3};
bool hasDup = false;
for (int i = 0; i < 4; i++)
for (int j = i + 1; j < 4; j++)
if (nums[i] == nums[j]) hasDup = true;
cout << "Has duplicate (naive O(n^2)): " << (hasDup ? "yes" : "no") << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
int[] nums = {1, 3, 5, 3};
boolean hasDup = false;
for (int i = 0; i < 4; i++)
for (int j = i + 1; j < 4; j++)
if (nums[i] == nums[j]) hasDup = true;
System.out.println("Has duplicate (naive O(n^2)): " + hasDup);
}
}
nums = [1, 3, 5, 3]
has_dup = False
for i in range(4):
for j in range(i + 1, 4):
if nums[i] == nums[j]:
has_dup = True
print("Has duplicate (naive O(n^2)):", has_dup)
#include <stdio.h>
int main() {
int nums[] = {1, 3, 5, 3};
int hasDup = 0;
for (int i = 0; i < 4; i++)
for (int j = i + 1; j < 4; j++)
if (nums[i] == nums[j]) hasDup = 1;
printf("Has duplicate (naive O(n^2)): %s\n", hasDup ? "yes" : "no");
return 0;
}
Login to try C/C++/Java code in the editor
Basic DSA Thinking
Code लिखने से पहले, तीन सवाल पूछने की आदत डालें: data किस shape का है, मुझे इस पर क्या operation perform करना है, और मुझे क्या result produce करना है? इनका पहले जवाब देना आपको guess करने के बजाय naturally सही data structure और algorithm की ओर इशारा करता है।
उदाहरण: Basic DSA Thinking
#include <iostream>
using namespace std;
int main() {
// Shape: array of ints. Operation: sum. Result: total.
int nums[] = {5, 10, 15};
int total = 0;
for (int i = 0; i < 3; i++) total += nums[i];
cout << "Total: " << total << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
// Shape: array of ints. Operation: sum. Result: total.
int[] nums = {5, 10, 15};
int total = 0;
for (int i = 0; i < 3; i++) total += nums[i];
System.out.println("Total: " + total);
}
}
# Shape: list of ints. Operation: sum. Result: total.
nums = [5, 10, 15]
total = sum(nums)
print("Total:", total)
#include <stdio.h>
int main() {
/* Shape: array of ints. Operation: sum. Result: total. */
int nums[] = {5, 10, 15};
int total = 0;
for (int i = 0; i < 3; i++) total += nums[i];
printf("Total: %d\n", total);
return 0;
}
Login to try C/C++/Java code in the editor
- यह सोचना कि DSA सिर्फ solutions याद करना है, जब goal एक problem को चाहिए operations के लिए सही data structure और algorithm चुनना है।
- किसी solution को सिर्फ इस आधार पर judge करना कि यह एक छोटे sample input पर काम करता है या नहीं, जब data बढ़ने पर एक naive approach बहुत धीमा हो सकता है।
- Data structures और algorithms को अलग treat करना, जब उदाहरण के लिए binary search सिर्फ इसलिए काम करता है क्योंकि array sorted है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: