खोज, न्यूनतम/अधिकतम और उलटना
रैखिक खोज लागू कीजिए, एक ही पास में न्यूनतम/अधिकतम मान निकालिए और ऐरे को उसी में या कॉपी बनाकर उलटिए
खोज, न्यूनतम/अधिकतम और उलटना, CoddyKit पर Java Academy का एक निःशुल्क पाठ है। यह 3 में से 2वाँ पाठ है। आप नीचे पूरा पाठ निःशुल्क पढ़ सकते हैं—फिर अंतर्निहित कोड संपादक और 24/7 एआई ट्यूटर के साथ ब्राउज़र में इसका व्यावहारिक अभ्यास कर सकते हैं। यह Java Academy सीखने के मार्ग का हिस्सा है और आपकी प्रगति वेब तथा CoddyKit ऐप पर सिंक होती रहती है। Java Academy पाठ्यक्रम में कुल 3 पाठ शामिल हैं।
रैखिक खोज
रैखिक खोज प्रत्येक तत्व को क्रम से जाँचती है, जब तक कि लक्ष्य न मिल जाए।
- लक्ष्य मिलने पर उसका अनुक्रमांक लौटाएँ
- न मिलने पर
-1लौटाएँ breakसे जल्दी रुक जाएँ
public class Main {
public static void main(String[] args) {
// Example array
int[] a = {5, 8, 9, 12, 15};
int t = 9; // target value to search
int idx = -1; // will stay -1 if not found
// Loop through each index of the array
for (int i = 0; i < a.length; i = i + 1) {
// Check if current element matches the target
if (a[i] == t) {
idx = i; // store the index where found
break; // exit loop early (found the target)
}
}
// Print result
if (idx != -1) {
System.out.println("Found " + t + " at index " + idx);
} else {
System.out.println(t + " not found in array");
}
}
}
findIndex()
खोज को एक विधि में समाहित करें:
public class Main {
public static void main(String[] args) {
// Example array
int[] a = {5, 8, 9, 12, 15};
int t = 9; // target value we are searching for
int idx = -1; // default index (-1 means "not found")
// Loop through each index of the array
for (int i = 0; i < a.length; i = i + 1) {
// Check if current element matches the target
if (a[i] == t) {
idx = i; // store the index where it was found
break; // stop searching (first match found)
}
}
// Print result
if (idx != -1) {
System.out.println("Found " + t + " at index " + idx);
} else {
System.out.println(t + " not found in array");
}
}
}
न्यूनतम/अधिकतम एक ही बार में
एक ही बार में न्यूनतम और अधिकतम निकालें:
public class Main {
public static void main(String[] args) {
// Example array
int[] a = {5, 8, 2, 12, 15, 1};
// Initialize min and max with the first element
int min = a[0];
int max = a[0];
// Loop starts from the second element (index 1)
for (int i = 1; i < a.length; i = i + 1) {
// If current element is smaller than current min → update min
if (a[i] < min) min = a[i];
// If current element is larger than current max → update max
if (a[i] > max) max = a[i];
}
// Print results
System.out.println("Minimum value = " + min);
System.out.println("Maximum value = " + max);
}
}
स्थान पर ही उलटना
दोनों सिरों के तत्वों की अदला-बदली करते हुए अंदर की ओर बढ़कर स्थान पर ही उलटें:
public class Main {
public static void main(String[] args) {
// Example array
int[] a = {5, 8, 9, 12, 15};
int i = 0; // start pointer (left side)
int j = a.length - 1; // end pointer (right side)
// Keep swapping until the two pointers meet
while (i < j) {
// Swap elements at positions i and j
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
// Move pointers toward the center
i = i + 1;
j = j - 1;
}
// Print the reversed array
System.out.print("Reversed array: ");
for (int k = 0; k < a.length; k++) {
System.out.print(a[k] + " ");
}
}
}
उलटी प्रति
उलटी प्रति बनाएँ (मूल ऐरे में कोई बदलाव नहीं होगा):
public class Main {
public static void main(String[] args) {
// Example array
int[] a = {5, 8, 9, 12, 15};
// Create a new array b with the same length
int[] b = new int[a.length];
// Copy elements from a into b in reverse order
for (int i = 0; i < a.length; i = i + 1) {
// (a.length - 1 - i) gives the reversed index
b[a.length - 1 - i] = a[i];
}
// Print original array
System.out.print("Original array: ");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(); // newline
// Print reversed array
System.out.print("Reversed array: ");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
}
}
खोज/MinMax/उलटने का प्रदर्शन
इसे चलाइए: खोज के परिणाम, न्यूनतम/अधिकतम मान और उलटे हुए ऐरे देखें।
public class Main {
static int findIndex(int[] a, int t) {
for (int i = 0; i < a.length; i = i + 1) {
if (a[i] == t) return i;
}
return -1;
}
static int min(int[] a) {
int m = a[0];
for (int i = 1; i < a.length; i = i + 1) {
if (a[i] < m) m = a[i];
}
return m;
}
static int max(int[] a) {
int m = a[0];
for (int i = 1; i < a.length; i = i + 1) {
if (a[i] > m) m = a[i];
}
return m;
}
static void reverseInPlace(int[] a) {
int i = 0, j = a.length - 1;
while (i < j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
i = i + 1;
j = j - 1;
}
}
static int[] reversedCopy(int[] a) {
int[] b = new int[a.length];
for (int i = 0; i < a.length; i = i + 1) {
b[a.length - 1 - i] = a[i];
}
return b;
}
static String join(int[] a) {
String s = "";
for (int i = 0; i < a.length; i = i + 1) {
s = s + a[i] + (i + 1 < a.length ? " " : "");
}
return s;
}
public static void main(String[] args) {
int[] a = {3, 1, 4, 1, 5};
System.out.println("findIndex 4 -> " + findIndex(a, 4));
System.out.println("findIndex 9 -> " + findIndex(a, 9));
System.out.println("min = " + min(a));
System.out.println("max = " + max(a));
int[] b = reversedCopy(a);
System.out.println("reversedCopy: " + join(b));
reverseInPlace(a);
System.out.println("reverseInPlace: " + join(a));
}
}
रैखिक खोज की जाँच
त्वरित जाँच: कौन-सा कोड-खंड a में t का अनुक्रमांक या -1 लौटाता है?
पुनरावृत्ति और अगला चरण
पुनरावृत्ति: आपने रैखिक खोज, न्यूनतम/अधिकतम और ऐरे उलटने के दो तरीकों को लागू किया।
अगला चरण: तत्वों को जोड़ना, हटाना (अनुक्रमांक से) और खिसकाना सीखें।
एआई शिक्षक के साथ Java सीखें — निःशुल्क
अपने ब्राउज़र में वास्तविक कोड लिखें और चलाएँ, चौबीसों घंटे एआई शिक्षक से तुरंत सहायता पाएँ, और वेब या ऐप पर वहीं से शुरू करें जहाँ आपने छोड़ा था।
- पाठ्यक्रम
- 104
- पाठ
- 374
अक्सर पूछे जाने वाले प्रश्न
क्या “खोज, न्यूनतम/अधिकतम और उलटना” पाठ निःशुल्क है?
हाँ—“खोज, न्यूनतम/अधिकतम और उलटना” का पूरा पाठ यहाँ वेब पर निःशुल्क पढ़ा जा सकता है। इंटरैक्टिव अभ्यास (अंतर्निहित कोड संपादक और 24/7 एआई ट्यूटर) करने और Java Academy पाठ्यक्रम का बाकी हिस्सा अनलॉक करने के लिए CoddyKit PRO लें। Java Academy पाठ्यक्रम में कुल 3 पाठ शामिल हैं।
“खोज, न्यूनतम/अधिकतम और उलटना” में मैं क्या सीखूँगा?
रैखिक खोज लागू कीजिए, एक ही पास में न्यूनतम/अधिकतम मान निकालिए और ऐरे को उसी में या कॉपी बनाकर उलटिए आप ब्राउज़र में सीधे चलाए जाने वाले व्यावहारिक कोड के साथ Java Academy का अभ्यास करते हैं, और पाठ पूरा करते समय 24/7 एआई ट्यूटर आपके प्रश्नों के उत्तर देता है।
क्या Java Academy शुरू करने के लिए मुझे किसी अनुभव की आवश्यकता है?
पहले के अनुभव की आवश्यकता नहीं है। CoddyKit पर Java Academy शुरुआती से लेकर उन्नत शिक्षार्थियों तक सभी के लिए व्यवस्थित किया गया है, इसलिए आप यहीं से या शुरुआत से सीखना शुरू कर सकते हैं और अपनी गति से आगे बढ़ सकते हैं। यह 3 में से 2वाँ पाठ है।
“खोज, न्यूनतम/अधिकतम और उलटना” पाठ पूरा करने में कितना समय लगता है?
CoddyKit का अधिकांश पाठ लगभग 5–10 मिनट में पूरा हो जाता है। हर पाठ छोटा और संवादात्मक है, इसलिए आप लगातार प्रगति करते हैं और वेब या ऐप पर वहीं से सीखना जारी रख सकते हैं जहाँ आपने छोड़ा था।
क्या मैं इस Java Academy पाठ में कोड लिख और चला सकता हूँ?
हाँ। हर Java Academy पाठ में एक अंतर्निर्मित कोड संपादक शामिल है, जिससे आप सीधे अपने ब्राउज़र में वास्तविक कोड लिख और चला सकते हैं और तुरंत एआई प्रतिक्रिया पा सकते हैं—स्थानीय सेटअप की आवश्यकता नहीं है।
इस पाठ्यक्रम के सभी पाठ
- ऐरे की बुनियादी बातें और पुनरावृत्ति
- खोज, न्यूनतम/अधिकतम और उलटना
- इंडेक्स से जोड़ना, हटाना और खिसकाना