البحث، والقيمتان الصغرى والكبرى، والعكس
نفّذ بحثًا خطيًا، واحسب القيمتين الصغرى والكبرى في مرور واحد، واعكس المصفوفات في موضعها أو عبر نسخة.
البحث، والقيمتان الصغرى والكبرى، والعكس درس مجاني في Java Academy على CoddyKit. هذا هو الدرس 2 من أصل 3. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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()
غلّفوا البحث داخل method:
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] + " ");
}
}
}
عرض البحث/الصغرى والعظمى/العكس
شغّله: شاهدوا نتائج البحث وقيمتي الصغرى والعظمى والمصفوفات المعكوسة.
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));
}
}
تحقق من البحث الخطي
تحقق سريع: أي مقطع يعيد فهرس t في a أو -1؟
مراجعة وما التالي
مراجعة: نفّذتم البحث الخطي وحساب القيمتين الصغرى والعظمى وطريقتين لعكس المصفوفات.
التالي: تعلّموا الإدراج والحذف (حسب الفهرس) وإزاحة العناصر.
الأسئلة الشائعة
هل درس «البحث، والقيمتان الصغرى والكبرى، والعكس» مجاني؟
نعم — نص درس «البحث، والقيمتان الصغرى والكبرى، والعكس» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Java Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Java Academy 3 دروس في المجموع.
ماذا ستتعلم في «البحث، والقيمتان الصغرى والكبرى، والعكس»؟
نفّذ بحثًا خطيًا، واحسب القيمتين الصغرى والكبرى في مرور واحد، واعكس المصفوفات في موضعها أو عبر نسخة. تتمرن على Java Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Java Academy؟
لا تُشترط خبرة سابقة. Java Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 3.
كم من الوقت يستغرق درس «البحث، والقيمتان الصغرى والكبرى، والعكس»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Java Academy هذا؟
نعم. كل درس في Java Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- أساسيات المصفوفات والتكرار
- البحث، والقيمتان الصغرى والكبرى، والعكس
- الإدراج والحذف حسب الفهرس والإزاحة