Skip to content

Commit 26ec7d2

Browse files
authored
chore: menambahkan algoritma linear searching (#11)
* chore: menambahkan algoritma linear searching * chore: Menambahkan algoritma linear searching
1 parent c35c1b5 commit 26ec7d2

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ if (NOT HAS_INTTYPES_H)
3939
endif()
4040

4141
add_subdirectory(math)
42-
add_subdirectory(algoritma)
42+
add_subdirectory(algoritma/sorting)
43+
add_subdirectory(algoritma/searching)
4344
add_subdirectory(hashing)
4445

4546
cmake_policy(SET CMP0054 NEW)

algoritma/searching/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
2+
foreach( testsourcefile ${APP_SOURCES} )
3+
string( REPLACE ".c" "" testname ${testsourcefile} )
4+
add_executable( ${testname} ${testsourcefile} )
5+
6+
if(OpenMP_C_FOUND)
7+
target_link_libraries(${testname} OpenMP::OpenMP_C)
8+
endif()
9+
if(MATH_LIBRARY)
10+
target_link_libraries(${testname} ${MATH_LIBRARY})
11+
endif()
12+
install(TARGETS ${testname} DESTINATION "bin/searching")
13+
14+
endforeach( testsourcefile ${APP_SOURCES} )
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @file linear_search.c
3+
* @brief implementasi algoritma linear searching.
4+
*
5+
* file ini berisi fungsi linear search untuk mencari target Dalam array integer
6+
* dan fungsi untuk mencetak array. Algoritma ini cocok untuk ukuran array
7+
* integer yang kecil.
8+
* */
9+
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
13+
/**
14+
* @brief mencari integer target dalam array integer menggunakan linear search.
15+
*
16+
* Fungsi ini akan mengembalikan indeks dari target yang diberikan di dalam array
17+
* integer tersebut. jika target tidak terdapat di dalam array, program akan
18+
* dihentikan
19+
*
20+
* @param array Array integer.
21+
* @param jumlah_element berupa integer dari array.
22+
* @param target berupa integer yang akan dicari di dalam array.
23+
*
24+
* @return int
25+
* */
26+
27+
int linear_search(int array[], int target, int jumlah_elemen) {
28+
for (int i = 0; i < jumlah_elemen; i++) {
29+
if (array[i] == target)
30+
return i;
31+
}
32+
return -1;
33+
}
34+
35+
36+
/**
37+
* @brief fungsi sederhana untuk mencetak keluaran dari array.
38+
*
39+
* @param array Array integer yang akan dicetak.
40+
* @param jumlah_elemen berupa integer dari array.
41+
*
42+
* @return void
43+
**/
44+
45+
void cetak_array(int array[], int jumlah_elemen) {
46+
for (int i = 0; i < jumlah_elemen; ++i) {
47+
printf("%d ", array[i]);
48+
}
49+
printf("\n");
50+
}
51+
52+
int main() {
53+
int jumlah_elemen = 6;
54+
int target = 100;
55+
int array[6] = { 30, 1, 91,63, 3, 32 };
56+
57+
int hasil = linear_search(array, target, jumlah_elemen); // hasil adalah indeks dari kembalian fungsi tersebut.
58+
59+
60+
printf("Dalam array: ");
61+
cetak_array(array, jumlah_elemen);
62+
63+
if (hasil < 0) {
64+
printf("target, yaitu %d, tidak ditemukan di dalam array\n", target);
65+
exit(1);
66+
}
67+
68+
printf("target, yaitu %d, berada di indeks ke %d\n", target, hasil);
69+
70+
return 0;
71+
}

algoritma/sorting/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
2+
foreach( testsourcefile ${APP_SOURCES} )
3+
string( REPLACE ".c" "" testname ${testsourcefile} )
4+
add_executable( ${testname} ${testsourcefile} )
5+
6+
if(OpenMP_C_FOUND)
7+
target_link_libraries(${testname} OpenMP::OpenMP_C)
8+
endif()
9+
if(MATH_LIBRARY)
10+
target_link_libraries(${testname} ${MATH_LIBRARY})
11+
endif()
12+
install(TARGETS ${testname} DESTINATION "bin/sorting")
13+
14+
endforeach( testsourcefile ${APP_SOURCES} )
File renamed without changes.

0 commit comments

Comments
 (0)