NumPy adalah bentuk singkatan dari Numerical Python. Ini digunakan untuk berbagai jenis operasi ilmiah dengan python. Numpy adalah perpustakaan besar dengan python yang digunakan untuk hampir semua jenis operasi ilmiah atau matematika. Itu sendiri merupakan array yang merupakan kumpulan dari berbagai metode dan fungsi untuk memproses array
NumPy Array - Menggeser elemen
Menggeser array termasuk menghapus elemen dari array dan mengembalikan elemen yang dihapus. Metode ini mengubah panjang array
Jika kita ingin menggeser elemen array NumPy, kita perlu menggunakan fungsi shift() dari perpustakaan scipy di mana defaultnya adalah membawa nilai konstanta dari luar array dengan nilai cval, atur di sini ke nan
Jumlah tempat di mana elemen digeser. Jika sebuah tupel, maka sumbu harus berupa tupel dengan ukuran yang sama, dan masing-masing sumbu yang diberikan digeser dengan angka yang sesuai. Jika sumbu int while adalah tupel int, maka nilai yang sama digunakan untuk semua sumbu yang diberikan
sumbu int atau tupel int, opsionalSumbu atau sumbu di mana elemen digeser. Secara default, larik diratakan sebelum digeser, setelah itu bentuk aslinya dipulihkan
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end0Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]2leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end2Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]0________1______4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end6Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]0leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end8________2______2________1______4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]7Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]2Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3
________6Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]_18_______7 3 4 5 6 7 1 2 8
3 4 5 6 7 1 2 _9
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]2 Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a) Elements are first moved in first set – (See below diagram for this movement1
________6Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]_22______3
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]1Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]9
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]9Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b) Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c) Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}1Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]0Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]43 4 5 6 7 1 2 8Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b) Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c) Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}7
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]_4
________6Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]_18_______0
________6Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]_18_______2
________6Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]_18_______4
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]1Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]9
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]9Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b) Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c) Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}1Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]0[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]4Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]0Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]43 4 5 6 7 1 2 8Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b) Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c) Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}7
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]_4
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]03
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]05
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]1Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b) Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c) Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}1Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]0[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]4
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]9Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b) Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c) Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}1Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]0Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]43 4 5 6 7 1 2 8Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b) Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c) Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}7
________6Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]_18_______7 3 4 5 6 7 1 2 8
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]_4
3 4 5 6 7 1 2 8Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]28Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]30___________leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end_6_______30Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]___________________________________________________________________________
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b) Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c) Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}1Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]43
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]51Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]52Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]53Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]54
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]_55
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]56Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]57Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]59
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]51Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]52Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]63Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]64
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]_65Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]66
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]71 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b) Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c) Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}1
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]51Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]52Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]63Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]64
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]_78
KeluaranOriginal array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]
Kompleksitas Waktu. O(log10(Setengah tidak ada elemen yang ada dalam larik yang diberikan)).
Ruang Bantu. O(1)
METODE 2 (Menggunakan array temp)
Tulis fungsi rotate(ar[], d, n) yang memutar arr[] dengan ukuran n kali d elemen.
Rotasi array di atas sebesar 2 akan membuat array
Python3
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]_79
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]2 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]81
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]83Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]85
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]87Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]9
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]1 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]92
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]94
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]87Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]87Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]2 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]87Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]9
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]1 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]07
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]09Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]11
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]87Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]87Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]2 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 [1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]4Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]2 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]25Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]27Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]2 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]83
________6Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]_18_______7 3 4 5 6 7 1 2 8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]_33
3 4 5 6 7 1 2 8Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]28Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]30___________leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end_6_______30Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________g EX
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]51Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]52Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]53Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]54Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]56Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]57
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]51Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]59Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]60Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]61Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]5Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]63
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]_64
KeluaranArray after left rotation is: [3, 4, 5, 6, 7, 1, 2]
Kompleksitas waktu. Pada)
Ruang Bantu. O(d)
METODE 3 (Putar satu per satu).
leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end_Untuk memutar satu per satu, simpan arr[0] dalam variabel sementara temp, pindahkan arr[1] ke arr[0], arr[2] ke arr[1] … dan akhirnya temp ke arr[n-1]
Mari kita ambil contoh yang sama arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2
Putar arr[] satu per 2 kali
Kita mendapatkan [2, 3, 4, 5, 6, 7, 1] setelah rotasi pertama dan [ 3, 4, 5, 6, 7, 1, 2] setelah rotasi kedua.
Python3
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]_65
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]2 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]67
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]69 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]87Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]71 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]72Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]73
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]75
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]76
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]2 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]78
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]83Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]82Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]9Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]44
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]69 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]87Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]71 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]72Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]90Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]0Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]93
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]09Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]97Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]2Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]44
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]02Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]0Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]44Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]83
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]_9
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]_09
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]2 Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]11
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]69 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]87Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]71 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]72Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]17
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]51 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]52Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]21Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]71 Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]23________6______8Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]25Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]57
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]_27
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]_28
3 4 5 6 7 1 2 8Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]28Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]30___________leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end_6_______30Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________g EX
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]46Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]5Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]30Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]41Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]57
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]51Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]41Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]57
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]_54
Keluaran3 4 5 6 7 1 2
Kompleksitas waktu. O(n * d)
Ruang Bantu. O(1)
METODE 4 (Algoritma Juggling)
Ini adalah perluasan dari metode 2. Alih-alih memindahkan satu per satu, bagilah array dalam set yang berbeda
di mana jumlah set sama dengan GCD dari n dan d dan pindahkan elemen di dalam set.
Jika GCD adalah 1 seperti untuk contoh array di atas (n = 7 dan d =2), maka elemen akan dipindahkan dalam satu set saja, kita mulai saja dengan temp = arr[0] dan terus bergerak arr[I+d]
Berikut adalah contoh untuk n =12 dan d = 3. GCD adalah 3 dan
Python3
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]_55
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]2 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]67
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]69 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]87Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]71 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]72Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]63
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]_9
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]66
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]83Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]09
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]72Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]87
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]1 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]66
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]79Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]80Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]72Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]2 [1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]79Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]56 Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]87Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]89
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]90Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]80Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]80Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]0 Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]95
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]79Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]56 Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]80Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8________6______8 leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end01
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]90leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end03
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]79leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end05Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end07
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]79Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]72Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]80
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end05Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]83
leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end_16
leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end_17
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]2 leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end19
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]69 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]87Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]71 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]72Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]17
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]9Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]51 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]52Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]21 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]71 leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end31________6______8Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]25Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]57
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]_27
leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end_36
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]2 leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end38
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]56 leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end41Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]9Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]66
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]93 4 5 6 7 1 2 7 leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end48
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]4Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]65Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]66
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]93 4 5 6 7 1 2 7 leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end54Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]71leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end56
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]_27
leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end_58
3 4 5 6 7 1 2 8Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]28Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]30___________leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end_6_______30Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________g EX
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]46Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]5Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]30Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]41Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]57
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]51Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]41Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]57
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]_54
Keluaran3 4 5 6 7 1 2
Kompleksitas waktu. Pada)
Ruang Bantu. O(1)
Pendekatan lain. Menggunakan Daftar mengiris
Python3
leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end_85
leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end_86
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]2 leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end88
leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end89Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]25Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end92Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]2_______2Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]82Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]9leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end96
leftRotate(arr[], d, n) start For i = 0 to i < d Left rotate all elements of arr[] by one end_893 4 5 6 7 1 2 7 3 4 5 6 7 1 2 8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]_33
3 4 5 6 7 1 2 8Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]8 Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]28Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]3Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]30Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]5Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]30Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]___________________________________________________________________________________f
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]_513 4 5 6 7 1 2 17
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]51Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]523 4 5 6 7 1 2 20Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]57
Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]513 4 5 6 7 1 2 23Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]5Original array: [1, 2, 3, 4, 5, 6, 7, 8] Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]30Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]603 4 5 6 7 1 2 27
3 4 5 6 7 1 2 _28
Keluaran[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]
Jika array perlu diputar lebih dari panjangnya maka mod harus dilakukan.
Misalnya. putar arr[] berukuran n demi d di mana d lebih besar dari n. Dalam hal ini d%n harus dihitung dan diputar dengan hasil setelah mod.